The normative pattern is to use DATE
datatype to store date values.
It appears you are storing your "date" values as VARCHAR, in format 'mm-dd-yy'. Comparisons of VARCHAR
expressions is performed character by character, from left to right. The character representation, with the month first, is not in canonical order, that is, a sort of the mm-dd-yy values will not be sorted in date order, e.g. '01-01-13' (Jan 2013) will sort before '11-27-12' (Nov 2012).
The simplest approach, in terms of a SQL statement, is to convert the VARCHAR into a DATE
datatype, and compare that to an expression that also returns a DATE
.
DELETE FROM `upcoming` WHERE STR_TO_DATE(`Date`,'%m-%d-%y') < CURDATE()
In terms of performance, MySQL can't perform an index range scan to satisfy this predicate. Every row in the table will need to be examined, to do the conversion, and then the comparison. If the column were stored as a DATE
, then the bare column could be referenced in the predicate and MySQL could use an index range scan.
ALSO: '%M'
format specifier returns the month name, 'January','February', etc. The '%m'
specifier returns numeric month '01','02'.