Mysql - How can I select rows by year if I have only the timestamp?
I have this table
Id Timestamp
1 | 1302566399
2 | 1502745635
3 | 1374679999
4 | 1264575886
5 | 1154645755
开发者_运维知识库How can I select only the rows where the year is 2011 ?
SELECT * FROM table WHERE FROM_UNIXTIME(timestamp, '%Y') = 2011;
SELECT * FROM t WHERE YEAR(`timestamp`) = 2011
The easiest way has been already suggested in rest of answers (directly from query)
You can do it this way too
$year_start = strtotime('2011-01-01');
$year_end = strtotime('2011-12-31 23:59:59.000');
$sql = "SELECT * FROM your_table
WHERE Timestamp>=$year_start AND Timestamp<=$year_end";
$res = mysql_query($sql);
function strtotime() depends of time zone set.
select * from table where from_unixtime(field,'%Y') = 2011
Try a query like this, where myTimestamp is the column with the timestamp in it
SELECT * FROM myTable WHERE FROM_UNIXTIME(myTimestamp, '%Y') = '2011'
精彩评论