php MySQL 仅选择过去 10 天的时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6932428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL select only where Timestamp is from last 10 days
提问by lisovaccaro
I want to limit my query to results that have been entered in the last 10 days. The TIMESTAMP column is called Date. How do I do it?
我想将查询限制为过去 10 天内输入的结果。TIMESTAMP 列称为日期。我该怎么做?
$result = mysql_query("SELECT * FROM Posts WHERE (City = '$city2') ORDER by Comments DESC LIMIT 5");
Thanks
谢谢
回答by Marc B
SELECT *
FROM Comments
WHERE (City = '$city2') AND (`Date` > DATE_SUB(now(), INTERVAL 10 DAY));
Note: calling a column 'Date' is poor practice, since it's a reserved word.
注意:调用“日期”列是不好的做法,因为它是一个保留字。
回答by Andrew
回答by corretge
Try with date_sub
尝试使用 date_sub
select * from Comments
where City = '{$city2}' and
`Date` > date_sub(now(), interval 10 day)
回答by rogersPass
Assuming your data is hourly can also use:
假设您的数据是每小时还可以使用:
(SELECT * FROM comments ORDER BY DateTime desc LIMIT 240) order by DateTime
I found this to be more exact in returning exactly 10 days to the hour.
我发现这更准确地将 10 天返回到小时。