【发布时间】:2021-12-02 02:22:59
【问题描述】:
我想检索表中最近 30 分钟的记录。怎么做?以下是我的查询..
select * from
[Janus999DB].[dbo].[tblCustomerPlay]
where DatePlayed < CURRENT_TIMESTAMP
and DatePlayed >
(CURRENT_TIMESTAMP-30)
【问题讨论】:
标签: sql sql-server tsql
我想检索表中最近 30 分钟的记录。怎么做?以下是我的查询..
select * from
[Janus999DB].[dbo].[tblCustomerPlay]
where DatePlayed < CURRENT_TIMESTAMP
and DatePlayed >
(CURRENT_TIMESTAMP-30)
【问题讨论】:
标签: sql sql-server tsql
更改此(CURRENT_TIMESTAMP-30)
致此:DateADD(mi, -30, Current_TimeStamp)
要获取当前日期,请使用 GetDate()。
【讨论】:
【讨论】:
用途:
SELECT *
FROM [Janus999DB].[dbo].[tblCustomerPlay]
WHERE DatePlayed < GetDate()
AND DatePlayed > dateadd(minute, -30, GetDate())
【讨论】:
DATEADD 仅在 MySQL 5.5.53 上返回 Function does not exist(我知道它已经过时了)
相反,我发现DatePlayed > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 30 minute) 产生了想要的结果
【讨论】:
请记住 CURRENT_TIMESTAMP - (number) 可以正常工作,但您需要了解它要查找的数字 - 它是浮点天数。所以 CURRENT_TIMESTAMP-1.0 是 1 天前,CURRENT_TIMESTAMP-0.5 是 1/2 天前。 30 分钟,即 1.0/48.0(使用基数,因此结果是浮点数)或 0.0208333333333333,因此如果将查询重写为
select * from
[Janus999DB].[dbo].[tblCustomerPlay]
where DatePlayed < CURRENT_TIMESTAMP
and DatePlayed >
CURRENT_TIMESTAMP-1.0/48.0
如果对您来说看起来更像 1/2 小时,您也可以使用 1.0/24.0/2.0。
【讨论】:
SQL Server 使用儒略日期,因此您的 30 表示“30 个日历日”。 getdate() - 0.02083 表示“30 分钟前”。
【讨论】: