mysql实战(五)—— 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


背景

想要删除某个表中的脏数据,但是提示报错“ 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”错误


总结

文章到这里就结束了,欢迎各位网友指点,点评。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值