Selecting Data within a Date/Time Range in MySQL
Query:
When attempting to retrieve data within a specified date range using MySQL's between operator, it's crucial to ensure the correct format of the values provided.
Example:
Consider the following query:
select * from hockey_stats where game_date between '11/3/2012 00:00:00' and '11/5/2012 23:59:00' order by game_date desc;
Despite having data within the specified time range, the query returns no results. This is because the values in the 'from' and 'to' fields are not in the correct datetime format.
Solution:
To fix this issue, the date format needs to be updated to the following:
select * from hockey_stats where game_date between '2012-03-11 00:00:00' and '2012-05-11 23:59:00' order by game_date desc;
By specifying the dates in the YYYY-MM-DD format, the query will accurately select data that falls within the specified date/time range.
The above is the detailed content of Why Can\'t I Select Data Within a Date/Time Range in MySQL?. For more information, please follow other related articles on the PHP Chinese website!