mysql:获取两个日期时间之间的记录数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5786649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql: get record count between two date-time
提问by gautamlakum
I am stuck with a problem in MySQL. I want to get the count of records between two date-time entries.
For example:
I have a column in my table named 'created' having the datetime
data type.
我在 MySQL 中遇到了问题。我想获取两个日期时间条目之间的记录数。
例如:
我的表中有一个名为“created”的列,它具有datetime
数据类型。
I want to count records which were created date-time between "TODAY'S 4:30 AM" and "CURRENT DATE TIME".
我想计算在“TODAY'S 4:30 AM”和“CURRENT DATE TIME”之间创建日期时间的记录。
I tried some of MySQL's functions but still no luck with it.
我尝试了一些 MySQL 的功能,但仍然没有运气。
Can you please help me with this? thanks.
你能帮我解决这个问题吗?谢谢。
回答by Harry Joy
May be with:
可能与:
SELECT count(*) FROM `table`
where
created_at>='2011-03-17 06:42:10' and created_at<='2011-03-17 07:42:50';
or use between
:
或使用between
:
SELECT count(*) FROM `table`
where
created_at between '2011-03-17 06:42:10' and '2011-03-17 07:42:50';
You can change the datetime as per your need. May be use curdate()
or now()
to get the desired dates.
您可以根据需要更改日期时间。可用于curdate()
或now()
获取所需的日期。
回答by Wes
select * from yourtable where created < now() and created > '2011-04-25 04:00:00'
回答by Shakti Singh
select * from yourtable
where created < now()
and created > concat(curdate(),' 4:30:00 AM')
回答by Aditya Tomar
for speed you can do this
为了速度,你可以这样做
WHERE date(created_at) ='2019-10-21'