mysql:获取两个日期时间之间的记录数

时间:2011-04-26 06:12:59

标签: mysql datetime date count

我遇到了MySQL的问题。我想得到两个日期时间条目之间的记录数 例如:
我的表中有一个名为“created”的列,其中包含datetime数据类型。

我想计算在“TODAY'S 4:30 AM”和“CURRENT DATE TIME”之间创建日期时间的记录。

我尝试了一些MySQL的功能,但仍然没有运气。

你能帮我解决这个问题吗? 感谢。

4 个答案:

答案 0 :(得分:70)

可以与:

SELECT count(*) FROM `table` 
where 
    created_at>='2011-03-17 06:42:10' and created_at<='2011-03-17 07:42:50';

或使用between

SELECT count(*) FROM `table` 
where 
    created_at between '2011-03-17 06:42:10' and '2011-03-17 07:42:50';

您可以根据需要更改日期时间。可以使用curdate()now()来获取所需的日期。

答案 1 :(得分:6)

select * from yourtable where created < now() and created > '2011-04-25 04:00:00'

答案 2 :(得分:5)

select * from yourtable 
   where created < now() 
     and created > concat(curdate(),' 4:30:00 AM') 

答案 3 :(得分:0)

为了速度,您可以这样做

WHERE date(created_at) ='2019-10-21'