1. 查询子句中的表名,不能和delete的表名一样
例如:
delete from tb_1 where score = ( select min(score) from tb_1)
执行时,会报错:1093 - You can't specify target table 'tb_1' for update in FROM clause, Time: 0.002000s
如果子查询的 from 子句和更新、删除对象使用同一张表,会出现上述错误。
解决方案:通过给from子句中的结果集起别名
delete from tb_1 where score = (select a.sc from (select min(score) as sc from tb_1) as a)
2. delete from tb_1 这样的子句中table不能使用别名
delete from tb_1 a where a.id = 8
以上sql执行时报错:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where a.id = 8' at line 1, Time: 0.002000s
解决方案:去掉别名
delete from tb_1 where id = 8
ps: select查询语句不影响,select * from tb_1 a where a.id = 4,可正常执行查询