Search code examples
mysqldatabasetimestamplimit

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 standard 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?


Solution

  • 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