首页 文章

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

提问于
浏览
32

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

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

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

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

3 回答

  • 5

    可能与:

    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() 来获取所需的日期 .

  • 65
    select * from yourtable where created < now() and created > '2011-04-25 04:00:00'
    
  • 6
    select * from yourtable 
       where created < now() 
         and created > concat(curdate(),' 4:30:00 AM')
    

相关问题