簡體   English   中英

SQL僅選擇最近5分鍾的記錄

[英]SQL select records only from last 5 minutes

我想獲取最近5分鍾的記錄。 我當前正在使用此代碼,該代碼是從此站點獲得的:

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

但這對我不起作用,它每隔5分鍾返回一次我的所有記錄。 例如,我將在2014-07-30 15:40:02擁有多個帶有時間戳的記錄,然后我將獲得5分鍾的間隔並在2014-07-30 15:45獲得多個帶有時間戳的記錄:02。

這是您的where子句:

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

在這種情況下,第一個界限大於第二個界限,因為您要從當前時間中減去5分鍾。 這實際上不應該匹配任何內容,因為:

where x between y and y - 5

應該總是返回一個空集。 邊界是有序的。 也許交換它們會有所幫助:

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

但是,如果您沒有將來的時間戳記,請執行以下操作:

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