Find records with a date field in the last 24 hours

user1022585 picture user1022585 · Nov 10, 2011 · Viewed 122.5k times · Source

In my SQL query how do i make it find the records in the last 24 hours? Eg

   SELECT * FROM news WHERE date < 24 hours

I usually do it by setting a variable to date() - 1 day and comparing it to that but I wondered whether the sql query way was faster?

Answer

a&#39;r picture a'r · Nov 10, 2011

You simply select dates that are higher than the current time minus 1 day.

SELECT * FROM news WHERE date >= now() - INTERVAL 1 DAY;