简体   繁体   中英

Select all records in the last 2 minutes - MySql

This is my mysql table

-----   -----------   -------------------
b_id    update_time   update_time_text
-----   -----------   -------------------
A:340   1436507350    10-07-2015 11:19:10
A:340   1436507707    10-07-2015 11:25:07
A:340   1436509704    10-07-2015 11:58:24
A:340   1436507828    10-07-2015 11:27:08

And this is my query

SELECT * FROM table WHERE b_id='A:340' AND update_time < (NOW() - 120) // 120 is 2 minutes

How do i select records in the last 2 mintues since my query is returning empty records and i'm not sure if i have to take unix time(update_time) or the readable time(update_time_text)

您可以使用date_subinterval 2 minute

SELECT * FROM table WHERE b_id='A:340' AND update_time < date_sub(NOW() - interval 2 minute) // 120 is 2 minutes

as update_time is a unix timestamp field then replace your NOW() with UNIX_TIMESTAMP() :

SELECT * FROM table WHERE b_id='A:340' AND update_time >= (UNIX_TIMESTAMP() - 120)

records older than 2 minutes:

SELECT * FROM table WHERE b_id='A:340' AND update_time < (UNIX_TIMESTAMP() - 120)

Try this query

SELECT * FROM table WHERE b_id='A:340' AND TIME_TO_SEC(update,NOW())<=120

If you want difference of time in second more detail refer this link

MySQL: how to get the difference between two timestamps in seconds

您可以尝试以下方法:

SELECT * FROM table WHERE b_id='A:340' AND update_time BETWEEN DATE_SUB(NOW(), INTERVAL 2 MINUTE) AND NOW()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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