In SQL, expression
x BETWEEN a AND b
literally means
a <= x AND x <= b
If a > b
, this will yield empty result no matter what.
In other words, order does matter - simply make sure that when using x BETWEEN a AND b
then a
must be <= b
.
For you it means swap dates (and fix some other errors as well):
SELECT * FROM mytable
WHERE create_date BETWEEN '2011-10-14'
AND NOW() - INTERVAL 60 DAY
UPDATE: One more note about using functions DATE_SUB()
or DATE_ADD()
.
In general, if you need to compare dates in big table, you should avoid using these functions at all costs! Modern MySQL supports date arithmetic using standard +
and -
operators, so expression
DATE_SUB(create_date, INTERVAL 60 DAY)
is equivalent to
create_date - INTERVAL 60 DAY
Not only this is easier to read, but it also allows server to take advantage of possible index you created for create_date
column. This speed-up cannot be applied to any function call like DATE_SUB()
- MySQL does not support functional indexes.