程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL 刪除數據庫中反復數據辦法小結

MySQL 刪除數據庫中反復數據辦法小結

編輯:MySQL綜合教程

MySQL 刪除數據庫中反復數據辦法小結。本站提示廣大學習愛好者:(MySQL 刪除數據庫中反復數據辦法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL 刪除數據庫中反復數據辦法小結正文


剛開端,依據我的設法主意,這個很簡略嘛,上sql語句

delete from zqzrdp where tel in (select min(dpxx_id) from zqzrdp group by tel having count(tel)>1);

履行,報錯!!~!~

異常意為:你不克不及指定目的表的更新在FROM子句。傻了,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; 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved