How do I query between two dates using MySQL?

NullVoxPopuli picture NullVoxPopuli · Sep 29, 2010 · Viewed 694.3k times · Source

The following query:

SELECT * FROM `objects` 
WHERE (date_field BETWEEN '2010-09-29 10:15:55' AND '2010-01-30 14:15:55')

returns nothing.

I should have more than enough data to for the query to work though. What am I doing wrong?

Answer

Daniel Vandersluis picture Daniel Vandersluis · Sep 29, 2010

Your second date is before your first date (ie. you are querying between September 29 2010 and January 30 2010). Try reversing the order of the dates:

SELECT *
FROM `objects`
WHERE (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')