bob*_*obo 4 mysql sql mysql-error-1093 sql-update sql-delete
错误1093表示如果子查询查询要删除的表,则无法使用子查询更新或删除.
所以你做不到
delete from table1 where id in (select something from table1 where condition) ;
Run Code Online (Sandbox Code Playgroud)
好吧,解决这个限制的最佳方法是什么(假设您确实需要子查询来执行删除,并且不能完全消除自引用子查询?)
编辑:
以下是对有兴趣的人的查询:
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 | | +---------+---------+------+-----+---------+-------+ -- The query is going to -- tell all my children to -- stop thinking my old parents -- are still their parents delete from adjacencies where parent in ( -- ALL MY PARENTS,grandparents select parent from adjacencies where child=@me and parent!=@me ) -- only concerns the relations of my -- grandparents WHERE MY CHILDREN ARE CONCERNED and child in ( -- get all my children select child from adjacencies where parent=@me ) ;
所以我到目前为止所尝试的是创建一个名为的临时表 adjsToDelete
create temporary table adjsToRemove( parent int, child int ) ;
insert into adjsToRemove...
Run Code Online (Sandbox Code Playgroud)
所以现在我有一个要删除的关系集合,其中父/子对各自唯一地标识要删除的行.但是如何从邻接表中删除每一对呢?
看来我需要为auto_increment每个条目添加一个唯一的ed键adjacencies,是吗?
在http://bugs.mysql.com/bug.php?id=6980中找到的解决方法对我有用,就是为将返回项目的子查询创建一个别名.所以
delete from table1 where id in
(select something from table1 where condition)
Run Code Online (Sandbox Code Playgroud)
会变成
delete from table1 where id in
(select p.id from (select something from table1 where condition) as p)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8999 次 |
| 最近记录: |