美文网首页
mysql 通过当前表条件搜索结果删除 [HY000][1093

mysql 通过当前表条件搜索结果删除 [HY000][1093

作者: 刘振宁的博客 | 来源:发表于2019-02-01 11:27 被阅读10次

    巨大的建筑,总是由一木一石叠起来的,我们何妨做做这一木一石呢?我时常做些零碎事,就是为此。
    这是对的,但是我没有说过这句话! —— 鲁迅

    问题

    mysql 通过当前表的搜索结果删除,不支持,如下:

    delete from truck_position where truck_id in (
        select tp.truck_id from truck_position tp
           where not exists(select 1 from truck_base tb where tb.id = tp.truck_id)
    )
    

    由于 truck_id 是通过 truck_postion表查询出来的,所以删除的时候不能删除,当然,oracle可以,这应该是mysql的一个bug。
    执行会报错如下:

    [HY000][1093] You can't specify target table 'truck_position' for update in FROM clause

    解决方法

    在select 外面包裹一层,就可以了,比如上面的处理方法:

    delete from truck_position where truck_id in (
       select truck_id from 
           (select tp.truck_id from truck_position tp
                  where not exists(select 1 from truck_base tb where tb.id = tp.truck_id)) 
       as a
    )
    

    这样他认为是从a表中查出来的,而不是从truck_position表中查出来的,哎,这解释器真是傻.

    相关文章

      网友评论

          本文标题:mysql 通过当前表条件搜索结果删除 [HY000][1093

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