简体   繁体   English

SQL仅选择最近5分钟的记录

[英]SQL select records only from last 5 minutes

I want to get the records from last 5 minutes. 我想获取最近5分钟的记录。 I am currently using this code, which I got from this site: 我当前正在使用此代码,该代码是从此站点获得的:

select * from table where timestamp between curtime() and timestamp(date_sub(now(), interval 5 minute))

But it doesn't work for me, it returns all my records at intervals of 5 minutes. 但这对我不起作用,它每隔5分钟返回一次我的所有记录。 For example, I'll have several records with a timestamp at 2014-07-30 15:40:02, then I'll get a 5 minute gap and get several records with a time stamp of 2014-07-30 15:45:02. 例如,我将在2014-07-30 15:40:02拥有多个带有时间戳的记录,然后我将获得5分钟的间隔并在2014-07-30 15:45获得多个带有时间戳的记录:02。

This is your where clause: 这是您的where子句:

where timestamp between curtime() and timestamp(date_sub(now(), interval 5 minute))

In this case, the first bound is larger than the second, because you are subtracting 5 minutes from the current time. 在这种情况下,第一个界限大于第二个界限,因为您要从当前时间中减去5分钟。 This actually should not match anything, because: 这实际上不应该匹配任何内容,因为:

where x between y and y - 5

should always return an empty set. 应该总是返回一个空集。 The bounds are in order. 边界是有序的。 Perhaps swapping them will help: 也许交换它们会有所帮助:

where timestamp between timestamp(date_sub(now(), interval 5 minute)) and curtime()

However, if you don't have future timestamps, just do: 但是,如果您没有将来的时间戳记,请执行以下操作:

where timestamp >= timestamp(date_sub(now(), interval 5 minute))
select * from table where timestamp >= timestamp(date_sub(now(), interval 5 minute))

尝试使用:

read=("""select * from table where timestamp >= timestamp(date_sub(now(), interval 5 minute))""")
select * from table where timestamp between curtime() and DateADD(minute, -5, curtime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM