简体   繁体   中英

Select records in next 30 minutes

I have the following structure in my queue MySQL table:

id | post_id | datetime_requested

I have a record in my table.

1 | 1234 | 2021-10-01 11:20:00

I want to run a SQL Query every 30 minutes to grab any records in the next 30 minutes.

So if the script ran at 11:00:00 it should grab the above record.

The below correctly gets the record:

SELECT * FROM queue WHERE datetime_requested >= ('2021-10-01 11:00' + interval 30 minute);

but this also works too, when it shouldn't:

SELECT * FROM queue WHERE datetime_requested >= ('2021-10-01 10:00' + interval 30 minute);

Why is that?

Your second query return records with a datetime_requested after 10:30 . This is why you get rows for 11:30 as it meets your criteria.

Maybe try with BETWEEN :

SELECT *
FROM queue
WHERE datetime_requested BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 30 MINUTE)

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