Answer the question
In order to leave comments, you need to log in
How to kill two birds with one mysql query?
Hello!
There are two mysql queries:
SELECT * FROM emails WHERE status=0 order by rand() limit 1
UPDATE emails SET status='1' WHERE id=5111
Answer the question
In order to leave comments, you need to log in
If you need to select data by parallel scripts, then this option works reliably:
UPDATE `emails` SET `status` = 1 WHERE @id := `id` AND `status` = 0 ORDER BY RAND() LIMIT 1;
SELECT * FROM `emails` WHERE `id` = @id;
UPDATE emails SET status='1' WHERE id=(SELECT id FROM emails WHERE status=0 order by rand() limit 1)
1. Set the proccess field of the double type to 0 in the table by default.
2. In the PHP script, set $process_id = microtime(TRUE) + getmypid(); to get a unique process ID for each script call.
3. UPDATE `emails` SET `process` = $process_id WHERE `process` = 0 LIMIT 1
4. SELECT * FROM `emails` WHERE `process` = $
process_id one data for processing. And you, as I understand it, solve just such a problem.
why would you want to block it. It seems to me that you want to solve your problem initially in the wrong way.
UPDATE emails SET status=1, id=LAST_INSERT_ID(id) order by RAND() LIMIT 1
to wrap a call of requests in transaction will not approach?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question