在網(wǎng)上查找刪除重復(fù)數(shù)據(jù)保留id最小的數(shù)據(jù),方法如下:
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
)
自己使用的時候顯示報錯:
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
暫時不知道是什么原因?qū)е碌摹?/p>
然后想辦法分布操作,首先篩選出有重復(fù)user的數(shù)據(jù),然后用max()選出其中較大的那一行:
SELECT max(id) from tb GROUP BY user HAVING count(user)>1
然后再根據(jù)得到的max(id)逐條刪除多余的數(shù)據(jù)
delete from tb where id=xx
是個笨方法,暫時先解決問題吧。
總結(jié)
以上所述是小編給大家介紹的Mysql刪除重復(fù)數(shù)據(jù)保留最小的id 的解決方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
您可能感興趣的文章:- MySQL清空數(shù)據(jù)表的方法實例與分析
- MySQL查詢重復(fù)數(shù)據(jù)(刪除重復(fù)數(shù)據(jù)保留id最小的一條為唯一數(shù)據(jù))
- mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實例詳解
- Mysql實現(xiàn)定時清空一張表的舊數(shù)據(jù)并保留幾條數(shù)據(jù)(推薦)