系列文章目录
You can't specify target table 'org_department' for update in FROM clause
背景
想要删除某个表中的脏数据,但是提示报错“ You can’t specify target table ‘org_department’ for update in FROM clause ”
1.查询脏数据的SQL
-- 查询脏数据
select * from org_department od left join department_info di on od.depId = di.id where di.id is null;
2.删除脏数据的SQL
delete from org_department where id in (select od.id from org_department od left join department_info di on od.depId = di.id where di.id is null)
具体实现过程
1.解决办法
通过因为这个不能根据查询一个表的查询结果作为条件更新这个表的数据,所以可以将这个表的查询结果作为一个临时表存在,然后再将这个临时表作为一个表存在,查询所需要的内容,作为原有表的查询条件
2.实例
实际运行的SQL语句
delete from org_department where id in (select id from (select od.* from org_department od left join department_info di on od.depId = di.id where di.id is null) as a)
执行上述SQL,实现了需求,不在报“ You can’t specify target table ‘org_department’ for update in FROM clause”错误
总结
文章到这里就结束了,欢迎各位网友指点,点评。