Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL Update value using a max date

I have this query, I want to be able to update the exec value to TRUE when my program finishes to execute a request and save it to my database to use it as a queue for when I have multiple executions, however, whenever I try with this query it gives me an error.

UPDATE motor 
SET exec=1 
where time=(SELECT max(time) 
            FROM motor 
            WHERE exec=0);

Error:

ERROR 1093 (HY000): You can't specify target table 'motor' for update in FROM clause

How can I do this?

like image 993
Edna Ramirez Avatar asked Dec 20 '22 13:12

Edna Ramirez


1 Answers

This is because your UPDATE could be cycling.

Use this code instead of :

UPDATE motor 
SET exec = 1 
WHERE exec = 0 
ORDER BY time DESC 
LIMIT 1;
like image 121
zessx Avatar answered Jan 03 '23 20:01

zessx