溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql數據庫去除重復數據的方法

發布時間:2020-09-30 14:27:10 來源:億速云 閱讀:265 作者:小新 欄目:MySQL數據庫

這篇文章主要介紹了mysql數據庫去除重復數據的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

mysql數據庫去除重復數據的方法:1、查詢需要刪除的記錄,會保留一條記錄;2、刪除重復記錄,只保留一條記錄,代碼為【delete a from test1 a, (...)as bid from test1 c where..】。

mysql數據庫去除重復數據的方法

mysql數據庫去除重復數據的方法:

1、查詢需要刪除的記錄,會保留一條記錄。

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、刪除重復記錄,只保留一條記錄。注意,subject,RECEIVER 要索引,否則會很慢的。

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、查找表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷

select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

4、刪除表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷,只留有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、刪除表中多余的重復記錄(多個字段),只留有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)

看來想偷懶使用一句命令完成這個事好像不太顯示,還是老老實實的分步處理吧,思路先建立復制一個臨時表,然后對比臨時表內的數據,刪除主表里的數據

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;

感謝你能夠認真閱讀完這篇文章,希望小編分享mysql數據庫去除重復數據的方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女