MySQL 解决错误 1093

标签 mysql sql sql-update sql-delete mysql-error-1093

错误 1093 指出如果您的子查询查询要从中删除的表,则您无法使用子查询进行更新或删除。

所以你不能这样做

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    |       |
+---------+---------+------+-----+---------+-------+



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

;

So what I've tried so far is creating a temporary table called adjsToDelete

create temporary table adjsToRemove( parent int, child int ) ;
insert into adjsToRemove...

现在我有一组要删除的关系,其中每个父/子对唯一标识要删除的行。但是现在如何从邻接表中删除每个

看来我需要为 adjacencies 中的每个条目添加一个唯一的 auto_incremented 键,对吗?

最佳答案

解决方法,在 http://bugs.mysql.com/bug.php?id=6980 中找到,对我有用的是为将返回项目的子查询创建一个别名。所以

delete from table1 where id in 
  (select something from table1 where condition)

将更改为

delete from table1 where id in
  (select p.id from (select something from table1 where condition) as p)

关于MySQL 解决错误 1093,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4484779/

相关文章:

Postgresql 在一条更新语句中更新 2 个表

mysql - 我无法对这部分 MySql 查询进行排序(升序/降序)?

mysql - SQL中的SHOW语句吗?

php - 谁能帮我将从表中的 mysql 数据库获取的数据传递到提交时的另一个页面?

java - 使用预处理语句设置表名

sql - 避免在温暖时显示重复的 Poptart 口味

python - 优化mysql多次更新

c# - 如果值存在则更新,否则将值插入数据库

mysql - 查询特定日期时间的数据库(没有秒或毫秒)

python - 通过获取 MySql 数组在 Python 中声明并填充数组