MySQL 刪除數(shù)據(jù)庫中重復(fù)數(shù)據(jù)方法小結(jié)
剛開始,根據(jù)我的想法,這個很簡單嘛,上sql語句
delete from zqzrdp where tel in (select min(dpxx_id) from zqzrdp group by tel having count(tel)>1);
執(zhí)行,報錯??!~!~

異常意為:你不能指定目標(biāo)表的更新在FROM子句。傻了,MySQL 這樣寫,不行,讓人郁悶。
難倒只能分步操作,蛋疼
以下是網(wǎng)友寫的,同樣是坑爹的代碼,我機器上運行不了。
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. 刪除重復(fù)記錄,只保留一條記錄。注意,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. 查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個字段(peopleId)來判斷
select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
4. 刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個字段(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.刪除表中多余的重復(fù)記錄(多個字段),只留有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)
看來想偷懶使用一句命令完成這個事好像不太顯示,還是老老實實的分步處理吧,思路先建立復(fù)制一個臨時表,然后對比臨時表內(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;
- mysql 數(shù)據(jù)表中查找重復(fù)記錄
- MySQL查詢重復(fù)數(shù)據(jù)(刪除重復(fù)數(shù)據(jù)保留id最小的一條為唯一數(shù)據(jù))
- 很全面的MySQL處理重復(fù)數(shù)據(jù)代碼
- MySQL數(shù)據(jù)庫中刪除重復(fù)記錄的方法總結(jié)[推薦]
- 有效查詢MySQL表中重復(fù)數(shù)據(jù)的方法和技巧分享
- MySQL 去除重復(fù)數(shù)據(jù)實例詳解
- 刪除MySQL重復(fù)數(shù)據(jù)的方法
- MySQL中刪除重復(fù)數(shù)據(jù)的簡單方法
- MYSQL刪除重復(fù)數(shù)據(jù)的簡單方法
- MySQL處理重復(fù)數(shù)據(jù)完整代碼實例
相關(guān)文章
MySQL數(shù)據(jù)庫的多種連接方式以及實用工具
mysql連接操作是客戶端進(jìn)程與mysql數(shù)據(jù)庫實例進(jìn)程進(jìn)行通信,下面這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫的多種連接方式以及實用工具的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
mysql5.7的安裝及Navicate長久免費使用的實現(xiàn)過程
這篇文章主要介紹了mysql5.7的安裝及Navicate長久免費使用的實現(xiàn)過程,本文給大家分享問題及解決方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
Mysql row number()排序函數(shù)的用法和注意
這篇文章主要介紹了Mysql row number()排序函數(shù)的用法和注意 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
MySQL學(xué)習(xí)第五天 MySQL數(shù)據(jù)庫基本操作
MySQL學(xué)習(xí)第五天我們將針對MySQL數(shù)據(jù)庫進(jìn)行基本操作,創(chuàng)建、修改、刪除數(shù)據(jù)庫等一系列操作進(jìn)行學(xué)習(xí),感興趣的小伙伴們可以參考一下2016-05-05
MySQL單表百萬數(shù)據(jù)記錄分頁性能優(yōu)化技巧
自己的一個網(wǎng)站,由于單表的數(shù)據(jù)記錄高達(dá)了一百萬條,造成數(shù)據(jù)訪問很慢,Google分析的后臺經(jīng)常報告超時,尤其是頁碼大的頁面更是慢的不行2016-08-08

