在網路上尋找刪除重複資料保留id最小的數據,方法如下:
DELETE FROM people WHERE peopleName IN ( SELECT peopleName FROM people GROUP BY peopleName HAVING count(peopleName) > 1 ) AND peopleId NOT IN ( SELECT min(peopleId) FROM people GROUP BY peopleName HAVING count(peopleName) > 1 )
自己使用的時候顯示報錯:
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
以上是Mysql刪除重複資料保留最小的id的詳細內容。更多資訊請關注PHP中文網其他相關文章!