![](/img/trans.png)
[英]How can I create a database each year and select the year I want for a select and display the records for that year?
[英]Mysql - How can I select rows by year if I have only the timestamp?
我有这张桌子
Id Timestamp
1 | 1302566399
2 | 1502745635
3 | 1374679999
4 | 1264575886
5 | 1154645755
如何仅选择年份为2011的行?
SELECT * FROM table WHERE FROM_UNIXTIME(timestamp, '%Y') = 2011;
已经在其余答案中建议了最简单的方法(直接来自查询)
你也可以这样
$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);
函数strtotime()取决于时区设置。
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.