简体   繁体   中英

How to select all records that are 10 minutes to timestamp?

I have timestamps in a table order_onsite_interpreter column called assg_real_timestamp .

I need to get all rows where assg_real_timestamp is 10 minutes before assg_real_timestamp.

The logic is this: I get all rows that are 10 minutes before the session starts(session starts as shown with assg_real_timestamp) and then I send a push notification to the user on a ios app. So if 10 minutes or less are before the session starts i have to fetch the rows.

This is an example, where I stop. I suspect I have made the wrong query.

SELECT * 
WHERE scheduling_type IN ('get_call', 'conference_call') 
AND push_notification_sent = 0 AND is_phone = 1 
AND assg_real_timestamp <= now() - INTERVAL 10 MINUTE

This is how to get all rows with assg_real_timestamp with a value set in the past 10 minutes.

SELECT * FROM `<table_name>`
WHERE scheduling_type IN ('get_call', 'conference_call') 
AND push_notification_sent = 0 
AND is_phone = 1 
AND assg_real_timestamp BETWEEN DATE_SUB(NOW() , INTERVAL 10 MINUTE) AND NOW()

Mysql: DATE_SUB()

See also: MySQL Select rows where timestamp column between now and 10 minutes ago

So if anyone has to retrieve all rows from the database, by looking at a columns timestamp that's in GMT, where the timestamp is less that 5 minutes from now()

SELECT orderID, scheduling_type, frm_lang, to_lang, customer_id, amount, 
 onsite_con_phone, assg_frm_date, assg_frm_st, timezone FROM 
`order_onsite_interpreter` WHERE scheduling_type IN ('conference_call', 
'get_call') AND push_notification_sent =0 AND is_phone =1 AND 
DATE_SUB(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(assg_frm_timestamp), '%Y-%c-%d 
%T:%f'), '-7:00', '-0:00'),INTERVAL 5 MINUTE) BETWEEN CONVERT_TZ(DATE_SUB(NOW(),
INTERVAL 5 MINUTE), '-7:00', '-0:00') AND CONVERT_TZ(NOW(), '-7:00', '-0:00') 

-7:00 is where I need to convert from my server's timezones to GMT timezone. It's a matter of syncing my values to GMT and them i can compare the timestamp and now(). This works, like this: A session starts at 02:03, meaning that in 02:03 - 00:05 = 01:58 the cron job that's fired every minute should retrieve the rows from order_onsite_interpreter table. After retrieving the row, I then send a push notification to user's app with message 'Your scheduled session is about start in 5 minutes, please login to the app'. And set push_notification_sent = 1 so the user doesn't receive the message anymore in the scope of the five minutes that the query from cron job finds.

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