0

I have a timestamp field and I want to select * where time [is within the last 24 hours]

What is the correct query for this?

Owen
  • 7,347
  • 12
  • 54
  • 73
  • Here's a simple example with 2 hours. Extending this to 24 is trivial http://stackoverflow.com/questions/589652/add-2-hours-to-current-time-in-mysql – Candide Sep 12 '11 at 15:24

1 Answers1

2
select * from your_table
where `time` between date_sub(now(), interval 1 day) and now();
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • I see what you mean. The example I posted was using date_add and hours. – Candide Sep 12 '11 at 15:31
  • **YES** the answer won't be make use of any index (if there is an index built on time field – ajreal Sep 12 '11 at 15:32
  • That's a cool query, thanks. Can I use other keywords for interval, such as hour, min, week etc? – Owen Sep 12 '11 at 15:59
  • yes, you can refer to the available unit http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add – ajreal Sep 12 '11 at 16:00