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

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

Susan Sarandon
Release: 2024-11-17 05:40:03
Original
890 people have browsed it

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;
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template