Home  >  Article  >  Database  >  How to Compare a DATE String with a DATETIME String in MySQL?

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

Barbara Streisand
Barbara StreisandOriginal
2024-11-14 09:20:02547browse

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'

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!

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