Home > Database > Mysql Tutorial > Why Can\'t I Select Data Within a Date/Time Range in MySQL?

Why Can\'t I Select Data Within a Date/Time Range in MySQL?

Susan Sarandon
Release: 2024-12-01 00:54:10
Original
557 people have browsed it

Why Can't I Select Data Within a Date/Time Range in MySQL?

Selecting Data within a Date/Time Range in MySQL

Query:

When attempting to retrieve data within a specified date range using MySQL's between operator, it's crucial to ensure the correct format of the values provided.

Example:

Consider the following query:

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

Despite having data within the specified time range, the query returns no results. This is because the values in the 'from' and 'to' fields are not in the correct datetime format.

Solution:

To fix this issue, the date format needs to be updated to the following:

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 specifying the dates in the YYYY-MM-DD format, the query will accurately select data that falls within the specified date/time range.

The above is the detailed content of Why Can\'t I Select 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