处理MySQL删除数据时Error Code: 1093. You can't specify target table '表名' for update in FROM clause
在执行:
delete from _Resume where Id in(
select A.Id from _Resume A
inner join _User B on A.UserId = B.UserId
select A.Id from _Resume A
inner join _User B on A.UserId = B.UserId
where A.CreateTime < B.CreateTime):
【详细错误】: Error Code: 1093. You can't specify target table '_Resume' for update in FROM clause.
【错误原因】:在更新或删除目标表中数据的时候如果使用子查询,目标表不能在子查询的From语句中出现~
原程序改造为:
1 create table tmp as
2 select A.Id from _Resume A
3 inner join _User B on A.UserId = B.UserId
4 where A.CreateTime < B.CreateTime;
5 delete from _Resume where Id in( select Id from tmp);
2 select A.Id from _Resume A
3 inner join _User B on A.UserId = B.UserId
4 where A.CreateTime < B.CreateTime;
5 delete from _Resume where Id in( select Id from tmp);
方法二:或者将子查询中的表使用别名.
Copyright © abandonship