在網(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
)
自己使用的時(shí)候顯示報(bào)錯(cuò):
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
暫時(shí)不知道是什么原因?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
是個(gè)笨方法,暫時(shí)先解決問(wèn)題吧。
總結(jié)
以上所述是小編給大家介紹的Mysql刪除重復(fù)數(shù)據(jù)保留最小的id 的解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
您可能感興趣的文章:- MySQL清空數(shù)據(jù)表的方法實(shí)例與分析
- MySQL查詢重復(fù)數(shù)據(jù)(刪除重復(fù)數(shù)據(jù)保留id最小的一條為唯一數(shù)據(jù))
- mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實(shí)例詳解
- Mysql實(shí)現(xiàn)定時(shí)清空一張表的舊數(shù)據(jù)并保留幾條數(shù)據(jù)(推薦)