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

How to Select Data Between a Date/Time Range in MySQL?

DDD
Release: 2024-11-23 02:27:11
Original
579 people have browsed it

How to Select Data Between a Date/Time Range in MySQL?

Selecting Data Between a Date/Time Range in MySQL

In MySQL, using the BETWEEN operator can facilitate selecting data that falls within a specific date or time range. When working with datetime columns, it is crucial to ensure that the values used in your query are formatted correctly.

The example provided in the question attempts to select data between two dates in 24-hour Zulu time format. However, an unexpected result is encountered due to an incorrect date format:

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

The dates in the query utilize a non-standard format, which causes the comparison to fail. To resolve this issue, the date values must be reformatted into a form recognizable by MySQL.

Correct Solution:

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

Here, the dates are adjusted to conform to the ISO 8601 standard, ensuring compatibility with MySQL's datetime datatype. This modification allows for the accurate selection of data within the specified date range.

The above is the detailed content of How to Select Data Between 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template