mysql 的delete from where 子查询的一些限制
1.使用mysql进行delete from操作时,若子查询的 FROM 字句和更新/删除对象使用同一张表,会出现错误。
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);(执行成功)
相关推荐
lerdor 2020-08-31
谢育政 2020-07-28
mrandy 2020-07-04
sunnyxuebuhui 2020-06-16
Laxcus大数据技术 2020-06-13
fyggzb 2020-06-10
vivenwan 2020-06-06
zhangwentaohh 2020-05-25
韩学敏 2020-05-25
Andrea0 2020-05-17
xx0cw 2020-05-16
林大鹏 2020-05-12
CHINA华军 2020-05-11
talkingDB 2020-05-05
lysanderK 2020-04-19
zuixin 2020-04-19
jimgreatly 2020-04-10