Home  >  Article  >  Database  >  How to Retrieve Data Within a Date/Time Range in MySQL?

How to Retrieve Data Within a Date/Time Range in MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-11-17 05:40:03804browse

How to Retrieve Data Within a Date/Time Range in MySQL?

Retrieving Data Within a Date/Time Range in MySQL

When retrieving data from a table based on a date/time range, it's essential to ensure that the datetime field is formatted correctly in the query.

The example provided encounters an issue because the date values in the 'from' and 'to' fields are not formatted in the standard MySQL datetime format (YYYY-MM-DD HH:MM:SS). Instead, they use a shorter format (MM/DD/YYYY HH:MM:SS) with zulu time.

To resolve this, the date values need to be converted to the correct format:

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 datetime format, MySQL will be able to accurately compare the game_date values with the specified date/time range and retrieve the desired data.

The above is the detailed content of How to Retrieve Data Within a Date/Time Range in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn