I have a mysql table with a datetime column. I want to fetch rows which are older than 24 hours but younger than 48 hours. Ive gone through the date time function not quite sure how to approach this or what to do in general.
+4
A:
Use:
SELECT *
FROM YOUR_TABLE t
WHERE t.datetime_column < DATE_SUB(NOW(), INTERVAL 24 HOUR)
AND t.datetime_column > DATE_SUB(NOW(), INTERVAL 48 HOUR)
OMG Ponies
2010-10-06 02:07:08
I was inspired to format my SQL like yours after you answered some of my SQL queries (haha, bad pun). Therefore, my (deleted) answer looks very similar to yours!
alex
2010-10-06 02:09:43
@alex: Thx, I just figure the easier it is to read--the easier it'll be to understand. Still, you can't be doing that bad if you come to the same answer. ...or, we both misunderstood the question =)
OMG Ponies
2010-10-06 02:17:50
@OMG Ponies I also got my head around `DATE_SUB()` and its friends based on some of your answers - so thank you!
alex
2010-10-06 02:22:37