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

How to Query Data within a Date/Time Range in MySQL?

Linda Hamilton
Release: 2024-11-25 14:39:11
Original
900 people have browsed it

How to Query Data within a Date/Time Range in MySQL?

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

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

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!

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