我已经写了这个查询
set sql_safe_updates=0;
delete from t1 where id < (select avg(id) from t1);
set sql_safe_updates=1;
但它给出了一个错误
Error Code: 1093. You can't specify target table 't1' for update in FROM clause
请解决。
最佳答案
您不能在查询的其余部分中指定删除
中使用的相同表。解决此问题的一种方法是使用 join
:
delete t
from t1 t join
(select avg(id) as avgid
from t1
)
on id < (select avg(id);
这是 MySQL 的限制。另一种解决方法是使用额外级别的子查询:
delete from t1 where id < (select avgid from (select avg(id) as avgid from t1));
这样做的原因是 MySQL 会具体化子查询。由于子查询的额外级别,编译器有点“忘记”它正在从修改的表 bing 中查询。
关于mysql - 错误代码: 1093.您无法指定目标表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206817/