E
E
Eugene2017-05-12 10:21:54
MySQL
Eugene, 2017-05-12 10:21:54

How to correctly compose SQL with date interval?

In short, the script runs every 15 minutes. Its task is to select all records (to run the notification script) that have a time-date (timestamp) field 30 minutes from the current one. Those. between (+ 30 minutes and + 45 minutes), but I did something with the request and it seems to me that this is not correct.

SELECT id FROM TABLE WHERE event_date between NOW() + INTERVAL 30 MINUTE and NOW() + INTERVAL 45 MINUTE

The problem is that the server is sometimes loaded and the script can sometimes work out of time, and then the NOW () point is shifted by seconds and some records are not included. And still there is a suspicion that the same record will be selected repeatedly.
Help me make a good request

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2017-05-12
@Rsa97

Due to different delay times when running the script, some entries may be selected several times, and some may not be selected at all. In addition, even when starting immediately, recording times that are multiples of 15 minutes will be selected twice since BETWEEN selects inclusive.

M
Max, 2017-05-12
@MaxDukov

Do you need not so much now() as the exact time? Those. 11:15:00? well, take the nearest exact time, plus do not between , but < and > .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question