mysql中 You can’t specify target table for update in FROM clause 解决方案

发布时间 2023-08-08 17:32:13作者: Sherlock先生

在mysql中更新数据,出现 You can't specify target table for update in FROM clause 错误,这句话意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。

update table set del_flag = '2' where id = #{id} OR dept_id IN (SELECT t.id FROM table t WHERE find_in_set(#{id}, ancestors)

解决方案:

再嵌套一层子查询

update table set del_flag = '2' where id = #{id} OR dept_id IN (select tt.id from (SELECT t.id FROM table t WHERE find_in_set(#{id}, ancestors)) tt)

 

在oracle就不会出现上面的错误。