Question:
When comparing dates using the DATE_FORMAT function, a user encountered unexpected results, with records before the comparison date appearing in the results. How can this issue be resolved?
Analysis:
The problem stems from comparing dates as strings rather than their actual date values. DATE_FORMAT converts dates into strings, which are then compared lexicographically, leading to incorrect results.
Solution:
To compare dates accurately, the query should compare date values directly, using the DATE() function to extract only the date information. The following modified query accomplishes this:
<code class="sql">select date_format(date(starttime),'%d-%m-%Y') from data where date(starttime) >= date '2012-11-02';</code>
In this query:
By comparing dates directly, the query effectively filters out records where the date is before '2012-11-02'.
The above is the detailed content of How to Resolve Incorrect Date Comparison Results Using DATE_FORMAT in MySQL?. For more information, please follow other related articles on the PHP Chinese website!