在网上查找删除重复数据保留id最小的数据,方法如下:
1 DELETE 2 FROM 3 people 4 WHERE 5 peopleName IN ( 6 SELECT 7 peopleName 8 FROM 9 people 10 GROUP BY 11 peopleName 12 HAVING 13 count(peopleName) > 1 14 ) 15 AND peopleId NOT IN ( 16 SELECT 17 min(peopleId) 18 FROM 19 people 20 GROUP BY 21 peopleName 22 HAVING 23 count(peopleName) > 1 24 )
自己使用的时候显示报错:
1 delete from tb where id in (SELECT max(id) from tb GROUP BY user HAVING count(user)>1)
[Err] 1093 - You can't specify target table ‘XXX’ for update in FROM clause
暂时不知道是什么原因导致的。
然后想办法分布操作,首先筛选出有重复user的数据,然后用max()选出其中较大的那一行:
SELECT max(id) from tb GROUP BY user HAVING count(user)>1
然后再根据得到的max(id)逐条删除多余的数据
1 delete from tb where id=xx
是个笨方法,暂时先解决问题吧。