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

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

Susan Sarandon
Susan SarandonOriginal
2024-11-24 15:25:13982browse

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

Selecting Data within a Date/Time Range in MySQL

In this inquiry, the user seeks assistance in extracting data within a specified date range from a database table containing a datetime column in 24-hour Zulu time format. However, the provided query is not yielding results, leading to the question of whether it is necessary to enforce the datetime data type for the 'from' and 'to' fields in the query.

To address the issue, consider the following:

The query you provided for selecting data between the date range '11/3/2012 00:00:00' and '11/5/2012 23:59:00' appears to be incorrect in the date formatting. For MySQL, the preferred date format is 'YYYY-MM-DD HH:MM:SS'.

To rectify this, update your query as follows:

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;

This revised query should provide you with the desired results, assuming the data in your database table is valid and within the specified date range.

The above is the detailed content of How to Select 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