Comparing date values of CURDATE() with a full tim

2019-02-25 13:27发布

问题:

I have a function that is placing timestamp values (YYYY-MM-DD HH:MM:SS) into META_VALUE column of table META.

What I want to do is to compare whether the date portion (YYYY-MM-DD) of the META_VALUE is equal to today (CURDATE()), disregarding the hour, minute and second (HH:MM:SS).

How can I accomplish this?

回答1:

SELECT * FROM table WHERE <timestamp-field> BETWEEN 'YYYY-MM-DD 00:00:00' AND 'YYYY-MM-DD 23:59:59'

Allways avoid doing calculations on the field if possible: e.g.

SELECT * FROM table WHERE DATE(<timestamp-field>) = 'YYYY-MM-DD'

will calculate DATE() for ALL rows in that table, so you are really talking wasted cycles here



回答2:

Simply use DATE:

SELECT * FROM table WHERE DATE(timestamp) = '2011-12-29'