Troubleshooting MySQL date range query issues
Question:
When using the BETWEEN operator to query a date range in a MySQL table, the query result is empty.
<code class="language-sql">SELECT * FROM `objects` WHERE (date_field BETWEEN '2010-09-29 10:15:55' AND '2010-01-30 14:15:55')</code>
Solution:
On closer inspection, the second date in the range ('2010-01-30 14:15:55') is before the first date ('2010-09-29 10:15:55'). This means that the query is requesting records between dates that are later than another date.
To correct this problem, reverse the order of the dates in the BETWEEN operator:
<code class="language-sql">SELECT * FROM `objects` WHERE (date_field BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')</code>
After making this correction, the query will now search for records with a date_field column value within the valid date range of '2010-01-30 14:15:55' and '2010-09-29 10:15:55'.
More resources:
The above is the detailed content of Why are My MySQL Date Range Queries Returning No Results?. For more information, please follow other related articles on the PHP Chinese website!