Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql DATETIME evaluation: Get all records whose value is before midnight of the current day (basically yesterday and before

Tags:

mysql

This is really simple yet I always struggle with it. I need help getting records before midnight:

 AND last_checked < date('2013-06-25 00:00:00'))

This obviously doesn't work, since its string evaluation. I do not want to restrict it to this year and put a between in the code. Any help is extremely appreciated :)

like image 988
user1955162 Avatar asked Jun 25 '13 21:06

user1955162


2 Answers

You can also do this in a generic way

AND last_checked < ( DATE(NOW()) + INTERVAL 0 SECOND );

Watch this:

mysql> SELECT DATE(NOW()) + INTERVAL 0 SECOND Midnight;
+---------------------+
| Midnight            |
+---------------------+
| 2013-06-25 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

mysql>
like image 147
RolandoMySQLDBA Avatar answered Sep 22 '22 06:09

RolandoMySQLDBA


You should be able to just do

AND last_checked < '2013-06-25 00:00:00'

Using the date() function just extracts the date part of the argument.

like image 22
Ryan Avatar answered Sep 21 '22 06:09

Ryan