美文网首页
mysql 批量删除错误分析

mysql 批量删除错误分析

作者: 寻梦的尕柳 | 来源:发表于2016-05-26 16:06 被阅读25次
delete t1 from tb_dict t1 
where t1.id in (
    select t2.id from tb_dict t2 where t2.nature = 'v-taxi'
);

使用这条 sql 语句进行批量删除时报 "You can't specify target table 't1' for update in FROM clause" 错误,查询后得知原来 msyql 不允许在子查询的同时删除原表中的数据。下面对应的解决办法。

delete t1 from tb_dict t1 
where t1.id in (
    select t3.id from (
        select * from tb_dict t2 where t2.nature = 'v-taxi'
    ) as t3
);

将子查询得到的数据封装成临时表,这时就能解决问题了。

相关文章

网友评论

      本文标题:mysql 批量删除错误分析

      本文链接:https://www.haomeiwen.com/subject/salxdttx.html