简体   繁体   English

Select 记录在接下来的 30 分钟内

[英]Select records in next 30 minutes

I have the following structure in my queue MySQL table:我的queue MySQL 表中有以下结构:

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.我想每 30 分钟运行一次SQL 查询,以获取接下来30 分钟内的任何记录。

So if the script ran at 11:00:00 it should grab the above record.因此,如果脚本在 11:00:00 运行,它应该获取上述记录。

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 .您的第二个查询在10:30之后返回带有datetime_requested的记录。 This is why you get rows for 11:30 as it meets your criteria.这就是为什么您会获得11:30的行,因为它符合您的条件。

Maybe try with BETWEEN :也许尝试使用BETWEEN

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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