在網上查找刪除重覆數據保留id最小的數據,方法如下: 自己使用的時候顯示報錯: [Err] 1093 - You can't specify target table ‘XXX’ for update in FROM clause 暫時不知道是什麼原因導致的。 然後想辦法分佈操作,首先篩選出有重覆u ...
在網上查找刪除重覆數據保留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
是個笨方法,暫時先解決問題吧。