Selecting Data within a Date/Time Range in MySQL
When attempting to retrieve data within a specific time range in MySQL, you may encounter issues if your date column is in a 24-hour zulu time format. The query that was used, shown below, returned no results:
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;
This is because MySQL requires the dates to be in a specific format, which is YYYY-MM-DD HH:MM:SS. To rectify this issue, you need to update the date format in your query:
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 using the correct format, the query will now accurately retrieve data within the specified date/time range.
The above is the detailed content of How to Query Data within a Date/Time Range in MySQL?. For more information, please follow other related articles on the PHP Chinese website!