mysql-delete中的查询⼦句
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⼦句中的结果集起别名
mysql中delete语句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,可正常执⾏查询

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。