简体   繁体   中英

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;

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 t WHERE YEAR(`timestamp`) = 2011
select * from table where from_unixtime(field,'%Y') = 2011

尝试这样的查询,其中myTimestamp是其中带有时间戳的列

SELECT * FROM myTable WHERE FROM_UNIXTIME(myTimestamp, '%Y') = '2011'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM