Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I query between two dates using MySQL?

Tags:

sql

mysql

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?

like image 923
NullVoxPopuli Avatar asked Sep 29 '10 14:09

NullVoxPopuli


People also ask

How do I query a date in MySQL?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column. (In our example, we use a column of the timestamp data type.)


1 Answers

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') 
like image 126
Daniel Vandersluis Avatar answered Sep 21 '22 01:09

Daniel Vandersluis