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
Linda HamiltonOriginal
2024-11-25 14:39:11914browse

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;

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!

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