想删除一张表中的一些重复数据,执行SQL为
delete from t where pid in (
select pid from t
group by pid HAVING count(1)>1
)
提示
[Err]1093-You can't specify target table 't' for update in FROM clause
意思是不能先select出同一表中的某些值,再update这个表(在同一语句中)。
这sql如果在oracle中没问题,但是MySQL的语句多少还是有些区别的,于是少做修改。
delete from t where pid in (
select temp.pid from (
select pid from t
group by pid HAVING count(1)>1
) temp
)
为什么这么修改?这里再查一次,然后重命名为temp,这样MySQL就检查不到是同一张表。
将select出的结果再通过中间表select一遍,这样就规避了错误。
注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。