开发者

Getting records from MySQL based on a time window

开发者 https://www.devze.com 2023-04-10 19:31 出处:网络
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 tim

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消