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

mysql数据库在执行同时查询本表数据并删除本表数据时候,报错!

报错原因:

DELETE from sys_user_function
    where User_Id = 19 and Function_Id in (
      select s.Function_Id from sys_user_function s where s.User_Id=19
    )

修改如下:

delete from sys_user_function where User_Id = 19 AND
Function_Id in 
(
select a.Function_Id from 
(
select max(Function_Id) Function_Id from sys_user_function a where a.User_Id=20 and EXISTS
(
select 1 from sys_user_function b where a.User_Id=b.User_Id group by User_Id HAVING count(1)>=1
)
group by Function_Id
) a
)

  

原文地址:https://www.cnblogs.com/yangyuke1994/p/9675035.html