您好,登錄后才能下訂單哦!
這篇文章主要介紹了mysql數(shù)據(jù)庫(kù)去除重復(fù)數(shù)據(jù)的方法,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
mysql數(shù)據(jù)庫(kù)去除重復(fù)數(shù)據(jù)的方法:1、查詢需要?jiǎng)h除的記錄,會(huì)保留一條記錄;2、刪除重復(fù)記錄,只保留一條記錄,代碼為【delete a from test1 a, (...)as bid from test1 c where..】。
mysql數(shù)據(jù)庫(kù)去除重復(fù)數(shù)據(jù)的方法:
1、查詢需要?jiǎng)h除的記錄,會(huì)保留一條記錄。
select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid
2、刪除重復(fù)記錄,只保留一條記錄。注意,subject,RECEIVER 要索引,否則會(huì)很慢的。
delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid;
3、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來(lái)判斷
select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
4、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來(lái)判斷,只留有rowid最小的記錄
delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
5、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有rowid最小的記錄
delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
看來(lái)想偷懶使用一句命令完成這個(gè)事好像不太顯示,還是老老實(shí)實(shí)的分步處理吧,思路先建立復(fù)制一個(gè)臨時(shí)表,然后對(duì)比臨時(shí)表內(nèi)的數(shù)據(jù),刪除主表里的數(shù)據(jù)
alter table tableName add autoID int auto_increment not null; create table tmp select min(autoID) as autoID from tableName group by Name,Address; create table tmp2 select tableName.* from tableName,tmp where tableName.autoID = tmp.autoID; drop table tableName; rename table tmp2 to tableName;
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享mysql數(shù)據(jù)庫(kù)去除重復(fù)數(shù)據(jù)的方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問(wèn)題就找億速云,詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。