开发者

How to limit mysql query and also limit it by a timestamp?

How could I take a mysql query like this:

SELECT id,title,timestamp,upvotes,downvotes,views FROM {$table} ORDER BY count DESC LIMIT $start_from, 20

and based on the timestamp (which is a stand开发者_JAVA技巧ard mysql timestamp)

make my mysql not only LIMIT $start_from, 20 but in addition...

Limit by:

if it was made today if it was made between today and a week ago if it was made between today and a month ago

Any ideas?


SELECT id, title, timestamp, upvotes, downvotes, views 
FROM {$table} 
WHERE timestamp >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH) 
ORDER BY count 
DESC LIMIT $start_from, 20

MySQL Reference Manual DATE_SUB


SELECT id,title,timestamp,upvotes,downvotes,views 
FROM {$table} 
where timestamp >= now() - interval 1 week

SELECT id,title,timestamp,upvotes,downvotes,views 
FROM {$table} 
where timestamp >= now() - interval 1 month 


Add a where clause to check the timestamp for the range you want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜