我正在寻找一个where子句,可用于检索过去24小时的记录?


当前回答

你好,我现在从原来的帖子过去了很多时间,但我有一个类似的问题,我想分享。

我得到了一个datetime字段,格式为YYYY-MM-DD hh:mm:ss,我想访问一整天,所以这是我的解决方案。

函数DATE(),在MySQL:提取日期或日期时间表达式的日期部分。

SELECT * FROM `your_table` WHERE DATE(`your_datatime_field`)='2017-10-09'

这一天我得到了所有的行寄存器。

我希望它能帮助到任何人。

其他回答

没有指定SQL 2005 / 2008

SELECT yourfields from yourTable WHERE yourfieldWithDate > dateadd(dd,-1,getdate())

如果您使用的是2008年增加的精度日期类型,则使用新的sysdatetime()函数,如果使用UTC时间内部交换到UTC调用,同样如此。

MySQL:

SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 24 HOUR)

时间间隔可以是年、月、日、小时、分、秒

比如,在最后十分钟

SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 10 MINUTE)
select ...
from ...
where YourDateColumn >= getdate()-1
SELECT * 
FROM tableName 
WHERE datecolumn >= dateadd(hour,-24,getdate())

在SQL Server中(过去24小时):

SELECT  *
FROM    mytable
WHERE   order_date > DateAdd(DAY, -1, GETDATE()) and order_date<=GETDATE()