mysql update select子查询

时间:2023-02-13 14:23:45

UPDATE user SET pwd='123' WHERE id in (
    SELECT id from user WHERE username = 'zx'
); 

报错[Err] 1093 - You can't specify target table 'user' for update in FROM clause

我们可以写内连接达到同样的效果 :

UPDATE USER a
INNER JOIN USER b ON b.username = 'zx'
AND a.id = b.id
SET a.pwd = 3