Home > Database > Mysql Tutorial > body text

How to Compare a DATE String with a DATETIME String in MySQL?

Barbara Streisand
Release: 2024-11-14 09:20:02
Original
546 people have browsed it

How to Compare a DATE String with a DATETIME String in MySQL?

Comparing DATE String with DATETIME String in MySQL

Users often require selecting data from a MySQL database by comparing a DATE string against strings stored as DATETIME. To achieve this, it's essential to consider the following approaches:

One can use the DATE() function to extract the date portion from the DATETIME field. This ensures the comparison is made only between the date components, effectively ignoring the time component. For instance, the following query would retrieve rows where the startTime column's date portion equals the provided DATE string:

SELECT * FROM `calendar` WHERE DATE(startTime) = '2010-04-29'
Copy after login

It's crucial to note that comparing DATE strings directly to DATETIME strings can lead to unexpected results, as MySQL treats DATETIME values differently. By using the DATE() function, the comparison is simplified to operate on date values, offering accurate results.

The above is the detailed content of How to Compare a DATE String with a DATETIME String 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