mysql 批量删除错误分析

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
);

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容