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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x