国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL 刪除數據庫中重復數據方法小結

2024-07-24 12:47:03
字體:
來源:轉載
供稿:網友

剛開始,根據我的想法,這個很簡單嘛,上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;


您可能感興趣的文章:

mysql查找刪除重復數據并只保留一條實例詳解Mysql刪除重復的數據 Mysql數據去重復MySQL中刪除重復數據的簡單方法刪除MySQL重復數據的方法MYSQL刪除重復數據的簡單方法MySQL數據庫中刪除重復記錄的方法總結[推薦]刪除mysql數據庫中的重復數據記錄Mysql刪除重復數據保留最小的id 的解決方法
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永嘉县| 安达市| 奉化市| 合阳县| 丘北县| 富锦市| 迁西县| 乾安县| 闽侯县| 韶关市| 浏阳市| 新丰县| 九台市| 民勤县| 马关县| 石柱| 安西县| 建阳市| 蒙阴县| 庄浪县| 朝阳县| 清河县| 永定县| 上饶县| 武胜县| 钦州市| 冕宁县| 华阴市| 临高县| 桑植县| 商城县| 瑞金市| 合阳县| 靖西县| 仲巴县| 长丰县| 渭源县| 时尚| 黎川县| 汝城县| 深圳市|