错误1093表示,如果您的子查询查询您正在删除的表,则无法使用子查询进行UPDATE或DELETE。
因此,您不能执行以下操作:
delete from table1 where id in (select something from table1 where condition) ;
好的,如果你确实需要子查询来执行删除操作且无法完全消除自引用子查询,那么绕过此限制的最佳方法是什么?(假设你确实需要子查询)
编辑:
对于那些感兴趣的人,以下是查询语句:
mysql> desc adjacencies ; +---------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------+------+-----+---------+-------+ | parent | int(11) | NO | PRI | NULL | | | child | int(11) | NO | PRI | NULL | | | pathLen | int(11) | NO | | NULL | | +---------+---------+------+-----+---------+-------+
-- 该查询将要求我所有的孩子停止认为我的旧父母仍然是他们的父母
delete from adjacencies where parent in ( -- 所有我的父母,祖父母等等 select parent from adjacencies where child=@me and parent!=@me )
-- 只关注我的祖父母与孙子女之间的关系 and child in ( -- 获取我所有的孩子 select child from adjacencies where parent=@me );
所以,到目前为止我尝试的方法是创建一个名为adjsToDelete的临时表。
create temporary table adjsToRemove( parent int, child int ) ;
insert into adjsToRemove...
现在我有一系列需要删除的关系,其中每个父/子对都唯一地标识了要删除的行。但是,我如何从相邻表中删除每个对呢?
看来我需要为adjacencies
中的每个条目添加一个唯一的auto_increment
键,对吗?