I want to count records based on a cut-off time. My cut-off time is midnight.
So, when a user inserts a new record, I want to know how many records have been inserted between NOW() and my cut-off time (which would be 12:00AM today). I don't know to tell MySQL to dynamically go 12:00AM of today... basically, what I want to do is:
SELECT COUNT(id) FROM myTable WHERE createdAt is between now and today开发者_运维技巧's 12:00AM
A query like this should do the trick.
SELECT COUNT(id) FROM myTable WHERE createdAt >= DATE(NOW()) AND createdAt <= NOW();
or simply
SELECT COUNT(id) FROM myTable WHERE createdAt >= DATE(NOW());
if you don't expect to have any records with createdAt date in the future. You can also use the "Between" operator as so.
SELECT COUNT(id) FROM myTable WHERE createdAt BETWEEN DATE(NOW()) AND NOW();
精彩评论