views:

113

answers:

4

Is there a way to select rows from a DB where the timestamp is in a certain year? I don't have a specific timestamp, just a range (ex. all timestamps within the year 2009). Is there a way to do this? How else might I go about doing something like this? Thanks for your help!

-iMaster

+1  A: 

Use FROM_UNIXTIME similar to this:

SELECT
  FROM_UNIXTIME(my_timestamp, '%Y') AS year
FROM
  table_name
WHERE
  FROM_UNIXTIME(my_timestamp, '%Y') = 2009;

Where 'my_timestamp' is the name of your timestamp column.

Alternatively you can also convert it to a DATETIME

If you convert it to datetime you can do it by using the mysql DATE_FORMAT function which allows you to take a DATETIME and format it as a date. Then group by that column.

  private function _formatDate() {
    if ($this->_granularity == 'month') {
      return '%y/%M';
    }elseif($this->_granularity == 'day') {
      return '%y/%M/%d';
    }
  } 

  public function getmyquery() {
    $query = "
 SELECT count( * ) as visits, DATE_FORMAT( `myOriginalDateField` , '".$this->_formatDate()."' ) AS mydate
 FROM `mys`
 WHERE id = ".$this->_Id."
 GROUP BY mydate
 ORDER BY mydate ASC
 ";
    return $query
  }
I don't understand what my_timestamp is. Is that the name of the timestamp column?
WillyG
Yes, replace it with name of your timestamp column.
+3  A: 

Use:

WHERE timestamp_col BETWEEN STR_TO_DATE('2009-01-01', '%Y-%m-%d') 
                        AND STR_TO_DATE('2009-12-31', '%Y-%m-%d')

Any functions performed on a column mean that an index, if one exists for that column, can not be used.

I used the STR_TO_DATE function to ensure that whatever date provided as a string could be interpreted by MySQL as a TIMESTAMP.

Reference:

OMG Ponies
Can it do variables? When I tried, like so:"SELECT title FROM Entries WHERE timestamp BETWEEN STR_TO_DATE($year.'-01-01', '%Y-%m-%d') AND STR_TO_DATE($year.'-12-31', '%Y-%m-%d') ORDER BY timestamp"I got an error...
WillyG
@iMaster: You need to enclose PHP variables within `{` and `}` IE: `STR_TO_DATE({$year}...` though it'd be better to construct the entire date as a PHP variable.
OMG Ponies
You're quotation marks are a bit messed up."SELECT title FROM Entries WHERE timestamp BETWEEN STR_TO_DATE($year.'-01-01', '%Y-%m-%d') AND STR_TO_DATE($year.'-12-31', '%Y-%m-%d') ORDER BY timestamp"------>"SELECT `title` FROM `Entries` WHERE `timestamp` BETWEEN STR_TO_DATE('$year-01-01', '%Y-%m-%d') AND STR_TO_DATE('$year-12-31', '%Y-%m-%d') ORDER BY `timestamp`"or better yet (as suggested by OMG Ponies) ::::::"SELECT `title` FROM `Entries` WHERE `timestamp` BETWEEN STR_TO_DATE('{$year}-01-01', '%Y-%m-%d') AND STR_TO_DATE('{$year}-12-31', '%Y-%m-%d') ORDER BY `timestamp`"
KillieTheBit
Another thing: "timestamp" is a reserved word in MySQL, so you should put it in backticks ``
KillieTheBit
@KillieTheBit: MySQL has an excemption for "timestamp" - see: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
OMG Ponies
Alright. I didn't catch that exception before :PThanks for the information.
KillieTheBit
+1  A: 

As simple as:

SELECT * FROM table WHERE YEAR(timestamp) = '1999'
code_burgar
If an index exists on the `timestamp` colum, that query won't be able to use it on account of the function (`YEAR`) being performed on the column.
OMG Ponies
True, yet on a table with almost 1 200 000 records it's still really fast: Showing rows 0 - 29 (1,191,950 total, Query took 0.0003 sec).
code_burgar
+1  A: 

An important addition to the excellent suggestions already given here: if you plan on executing this query as a part of rendering your pages (as opposed to running this as a one-off report), you should really consider performance. Indexes won't help you much if you're post-processing the column value with a function before comparing it to something.

In that case, I would consider creating a separate database column that contains JUST the year, or just the month, etc.

Alex