Reputation: 132
I have a problem when I try to update a value in a row database. I need update (add 1 hour) to especifics users in a USER table.
SELECT DATE_ADD(dateOfBirth, INTERVAL 1 HOUR), dateOfBirth
from USER
where dateOfBirth like '%23:00:00%';
But when I execute the next UPDATE, show a error
UPDATE USER
set dateOfBirth = DATE_ADD(dateOfBirth, INTERVAL 1 HOUR)
where id in (select u.id FROM USER u where u.dateOfBirth like '%23:00:00%');
Error:
Error Code: 1093. You can't specify target table 'USER' for update in FROM clause
It's possible execute this sentence with other way?
Upvotes: 1
Views: 79
Reputation: 384
The subquery in the update is not required.
UPDATE USER
set dateOfBirth = DATE_ADD(dateOfBirth, INTERVAL 1 HOUR)
where dateOfBirth like '%23:00:00%';
Upvotes: 3
Reputation: 5403
What's wrong with:
UPDATE USER
SET dateOfBirth = DATE_ADD(dateOfBirth, INTERVAL 1 HOUR)
WHERE dateOfBirth like '%23:00:00%';
Upvotes: 2
Reputation: 1269633
Isn't the id
unique in the table? If so:
update user u
set dateOfBirth = DATE_ADD(dateOfBirth, INTERVAL 1 HOUR)
where time(u.dateOfBirth) = '23:00:00';
Note that the dateOfBirth
is apparently a date/time column (why it contains a time I don't understand). So, date/time functions are more appropriate for the comparison.
Upvotes: 1