covfefe covfefe - 2 years ago 60
MySQL Question

select records with date in the last 24 hours

I want to select from my table all records where date (datetime mysql format YYYY-MM-DD HH:MM:SS) is in the last 24 hours. I have a query, but it doesn't completely work

SELECT * FROM `my_table` WHERE date > DATE_SUB(NOW(), INTERVAL 24 HOUR)


why it returns the date like that 2013-07-01 12:00:00. How would I do this? Thanks.

Answer Source

You already have a lower limit on the date, but since your table can have future dates you also need an upper limit. This should work:

SELECT *
FROM my_table
WHERE date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
  AND date <= NOW()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download