删除所有重复的电子邮箱,只保留一个具有最小id的唯一电子邮箱。
delete from Person where id not in(
select id from (
select min(id) id from Person group by email
) a
)
在MYSQL中,不能先Select一个表的记录,再按此条件Update和Delete同一个表的记录,否则会出错:You can't specify target table 'xxx' for update in FROM clause。
解决方法:使用嵌套Select——将Select得到的查询结果作为中间表,再Select一遍中间表作为结果集,即可规避错误。