Home > Blockchain >  Select records in next 30 minutes
Select records in next 30 minutes

Time:10-02

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?

CodePudding user response:

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)
  • Related