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!