本篇文章给大家带来的内容是关于mysql中delete from where子查询的限制介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、使用mysql进行delete from操作时,若子查询的 FROM 字句和更新/删除对象使用同一张表,会出现错误。(相关推荐:MySQL教程)
mysql> DELETE FROM 'tab' where id in (select min(id) from tag GROUP BY field1,field2 HAVING COUNT(id)>1);
error: You can't specify target table 'tab' for update in FROM clause.(不能为FROM子句中的更新指定目标表'tab')
针对“同一张表”这个限制,多数情况下都可以通过多加一层select 别名表来变通解决,像这样DELETE FROM 'tab' where id in
(
select id from
(
select max(id) from 'tab' GROUP BY field1,field2 HAVING COUNT(id)>1
) ids
);
2.delete from table... 这其中table不能使用别名mysql> delete from table a where a.id in (1,2);(语法错误)
mysql> select a.* from table a where a.id in (1,2);(执行成功)