MySql中You can't specify target table for update in FROM clause

发布时间 2023-08-11 14:35:46作者: 啊俊同学

MySql中You can't specify target table for update in FROM clause

问题描述:当我执行下面这段语句时,出现了这个bug

UPDATE account set status=1 where id in ( 
select id from account where ISNULL(status));

出现bug:You can't specify target table for update in FROM clause

造成原因:不能先select出同一表中的某些值,再update这个表(在同一语句中),意思是不能将account 查出来的字段当做update的where条件。

解决方法:在select外边套一层,让数据库认为你不是查同一表的数据作为同一表的更新数据。

UPDATE account set status=1 where id in (
select temp.id from 
(select id from account where ISNULL(status)) temp);

注:该错误只有Mysql会出现,MSSQL和Oracle不会出现该错误