开发者

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

开发者 https://www.devze.com 2023-03-10 02:46 出处:网络
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

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

精彩评论

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