开发者

MySQL select only where Timestamp is from last 10 days

开发者 https://www.devze.com 2023-03-25 12:47 出处:网络
I want to limit my query to results that have been entered in the last 10 days. The TIMESTAMP column is called D开发者_开发技巧ate. How do I do it?

I want to limit my query to results that have been entered in the last 10 days. The TIMESTAMP column is called D开发者_开发技巧ate. How do I do it?

$result = mysql_query("SELECT * FROM Posts WHERE (City = '$city2') ORDER by Comments DESC LIMIT 5");

Thanks


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.


You can use DATEDIFF or, as already posted, DATE_SUB. Also, I suggest not using reserved words like "Date" for column names. Example for your code:

WHERE DATEDIFF(NOW(), `Date`) < 10


Try with date_sub

select * from Comments 
where City = '{$city2}' and 
`Date` > date_sub(now(), interval 10 day)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消