A
A
aopil2021-04-05 15:03:57
PHP
aopil, 2021-04-05 15:03:57

How to exclude selections of one and a row of sql?

Making a fetch from the database

SELECT * FROM table WHERE Type =:type AND DateTimeRequest < NOW() - INTERVAL 24 HOUR ORDER BY DateTimeRequest ASC LIMIT 1

Next, I update the time:
UPDATE table SET DateTimeRequest = NOW() WHERE Id = $id


If the script is run 2 times at the same time, then sometimes it selects the same line, thereby pulling out the same result, how to avoid this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
entermix, 2021-04-05
@entermix

Use transactions

F
feniksdv, 2021-04-05
@feniksdv

Do a mutable field check

B
Boris Korobkov, 2021-04-05
@BorisKorobkov

Bad option: use lock.
for cron: flock
for mysql: https://dev.mysql.com/doc/refman/8.0/en/lock-table...
for postgresql: https://www.postgresql.org/docs/9.1/functions- admi...
A good option: a complete rewrite with the involvement of a competent developer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question