如何使用 SQL 从过去 24 小时中选择记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1888544/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:44:15  来源:igfitidea点击:

How to select records from last 24 hours using SQL?

sqldatetime

提问by Mike

I am looking for a whereclause that can be used to retrieve records for the last 24 hours?

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

采纳答案by Guillaume Flandre

SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)

回答by Quassnoi

In MySQL:

MySQL

SELECT  *
FROM    mytable
WHERE   record_date >= NOW() - INTERVAL 1 DAY

In SQL Server:

SQL Server

SELECT  *
FROM    mytable
WHERE   record_date >= DATEADD(day, -1, GETDATE())

In Oracle:

Oracle

SELECT  *
FROM    mytable
WHERE   record_date >= SYSDATE - 1

In PostgreSQL:

PostgreSQL

SELECT  *
FROM    mytable
WHERE   record_date >= NOW() - '1 day'::INTERVAL

In Redshift:

Redshift

SELECT  *
FROM    mytable
WHERE   record_date >= GETDATE() - '1 day'::INTERVAL

In SQLite:

SQLite

SELECT  *
FROM    mytable
WHERE   record_date >= datetime('now','-1 day')

In MS Access:

MS Access

SELECT  *
FROM    mytable
WHERE   record_date >= (Now - 1)

回答by Karthik Jayapal

MySQL :

MySQL:

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

The INTERVAL can be in YEAR, MONTH, DAY, HOUR, MINUTE, SECOND

INTERVAL 可以是 YEAR、MONTH、DAY、HOUR、MINUTE、SECOND

For example, In the last 10 minutes

例如,在过去 10 分钟内

SELECT * 
FROM table_name
WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 10 MINUTE)

回答by Andrew

Which SQL was not specified, SQL 2005 / 2008

未指定哪个 SQL,SQL 2005 / 2008

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

If you are on the 2008 increased accuracy date types, then use the new sysdatetime() function instead, equally if using UTC times internally swap to the UTC calls.

如果您使用的是 2008 年精度更高的日期类型,那么请改用新的 sysdatetime() 函数,同样如果使用 UTC 时间在内部交换到 UTC 调用。

回答by pisaruk

in postgres, assuming your field type is a timestamp:

在 postgres 中,假设您的字段类型是时间戳:

select * from table where date_field > (now() - interval '24 hour');

select * from table where date_field > (now() - interval '24 hours');

回答by lightup

If the timestamp considered is a UNIX timestamp You need to first convert UNIX timestamp (e.g 1462567865) to mysql timestamp or data

如果考虑的时间戳是 UNIX 时间戳,则需要先将 UNIX 时间戳(例如 1462567865)转换为 mysql 时间戳或数据

SELECT * FROM `orders` WHERE FROM_UNIXTIME(order_ts) > DATE_SUB(CURDATE(), INTERVAL 1 DAY)

回答by Mladen Prajdic

select ...
from ...
where YourDateColumn >= getdate()-1

回答by Rafa cosquiere

Hello i now it past a lot of time from the original post but i got a similar problem and i want to share.

你好,我现在已经过了很多时间,但我遇到了类似的问题,我想分享一下。

I got a datetime field with this format YYYY-MM-DD hh:mm:ss, and i want to access a whole day, so here is my solution.

我有一个格式为 YYYY-MM-DD hh:mm:ss 的日期时间字段,我想访问一整天,所以这是我的解决方案。

The function DATE(), in MySQL: Extract the date part of a date or datetime expression.

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

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

with this i get all the row register in this day.

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

I hope its help anyone.

我希望它可以帮助任何人。

回答by Galwegian

SELECT * 
FROM tableName 
WHERE datecolumn >= dateadd(hour,-24,getdate())

回答by arunkumar.halder

In SQL Server (For last 24 hours):

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

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