mysql數(shù)據(jù)庫刪除重復(fù)數(shù)據(jù)只保留一條方法實(shí)例
1.問題引入
假設(shè)一個(gè)場景,一張用戶表,包含3個(gè)字段。id,identity_id,name?,F(xiàn)在身份證號identity_id和姓名name有很多重復(fù)的數(shù)據(jù),需要?jiǎng)h除只保留一條有效數(shù)據(jù)。
2.模擬環(huán)境
1.登入mysql數(shù)據(jù)庫,創(chuàng)建一個(gè)單獨(dú)的測試數(shù)據(jù)庫mysql_exercise
create database mysql_exercise charset utf8;
2.創(chuàng)建用戶表users
create table users( id int auto_increment primary key, identity_id varchar(20), name varchar(20) not null );
3.插入測試數(shù)據(jù)
insert into users values(0,'620616199409206512','張三'), (0,'620616199409206512','張三'), (0,'62062619930920651X','李四'), (0,'62062619930920651X','李四'), (0,'620622199101206211','王五'), (0,'620622199101206211','王五'), (0,'322235199909116233','趙六');
可以多執(zhí)行幾次,生成較多重復(fù)數(shù)據(jù)。
4.解決思路
(1)根據(jù)身份證號和name進(jìn)行分組;
(2)取出分組后的最大id(或最小id);
(3)刪除除最大(或最?。﹊d以外的其他字段;
5.第一次嘗試(失敗!!!)
delete from users where id not in (select max(id) from users group by identity_id,name);
報(bào)錯(cuò):
1093 (HY000): You can't specify target table 'users' for update in FROM clause
因?yàn)樵贛YSQL里,不能先select一個(gè)表的記錄,再按此條件進(jìn)行更新和刪除同一個(gè)表的記錄。
解決辦法是,將select得到的結(jié)果,再通過中間表select一遍,這樣就規(guī)避了錯(cuò)誤,
這個(gè)問題只出現(xiàn)于mysql,mssql和oracle不會出現(xiàn)此問題。
所以我們可以先將括號里面的sql語句先拿出來,先查到最大(或最小)id。
select max_id from (select max(id) as max_id from users group by identity_id,name);
接著,又報(bào)錯(cuò)了!??!
ERROR 1248 (42000): Every derived table must have its own alias
意思是說:提示說每一個(gè)衍生出來的表,必須要有自己的別名!
執(zhí)行子查詢的時(shí)候,外層查詢會將內(nèi)層的查詢當(dāng)做一張表來處理,所以我們需要給內(nèi)層的查詢加上別名
繼續(xù)更正:
給查詢到的最大(或最小id)結(jié)果當(dāng)做一張新的表,起別名t,并查詢t.mix_id。
select t.max_id from (select max(id) as max_id from users group by identity_id,name) as t;
可以成功查到最大(或最小)id了,如下圖:
6.第二次嘗試(成功?。。。?/strong>
delete from users where id not in ( select t.max_id from (select max(id) as max_id from users group by identity_id,name) as t );
執(zhí)行結(jié)果:
成功將重復(fù)的數(shù)據(jù)刪除,只保留了最后一次增加的記錄。同理也可以保留第一次添加的記錄(即刪除每個(gè)分組里面除最小id以外的其他條記錄)
3.知識拓展一:更新數(shù)據(jù)
其他場景應(yīng)用:要將用戶表user_info里名字(name)為空字符串("")的用戶的狀態(tài)(status)改成"0"
update user_info set status='0' where user_id in (select user_id from user_info where name='')
同樣報(bào)了如下錯(cuò)誤:
You can't specify target table ‘user_info' for update in FROM clause
因?yàn)樵贛YSQL里,不能先select一個(gè)表的記錄,再按此條件進(jìn)行更新和刪除同一個(gè)表的記錄,解決辦法是,將select得到的結(jié)果,再通過中間表select一遍,這樣就規(guī)避了錯(cuò)誤。
以下兩種均可?。?!
update user_info set status='0' where user_id in (select user_id from (select user_id from user_info where name = '') t1);
下面這種也可,細(xì)微差別,別名可帶as可不帶,t1.user_id 直接和內(nèi)層的user_id對應(yīng)也可以。
update user_info set status='0' where user_id in (select t1.user_id from (select user_id from user_info where name='') as t1);
3.1 分步驟解析
(1)將以下查詢結(jié)果作為中間表:
select user_id from user_info where name='';
(2)再查詢一遍中間表作為結(jié)果集:
select user_id from (select user_id from user_info where name='') as t;
(3)更新數(shù)據(jù)
update user_info set status='0' where user_id in (select user_id from (select user_id from user_info where name='') as t1);
4.拓展練習(xí):刪除重復(fù)數(shù)據(jù)
編寫一個(gè) SQL 查詢,來刪除 Person 表中所有重復(fù)的電子郵箱,重復(fù)的郵箱里只保留 Id 最小 的那個(gè)。
+----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+
Id 是這個(gè)表的主鍵。
例如,在運(yùn)行你的查詢語句之后,上面的 Person 表應(yīng)返回以下幾行:
+----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | +----+------------------+
解答一:
delete from Person where Id not in ( select t.min_id from ( select min(Id) as min_id from Person group by Email ) as t );
解答二:
delete p1 from Person as p1,Person as p2 where p1.Email=p2.Email and p1.Id > p2.Id;
總結(jié)
到此這篇關(guān)于mysql數(shù)據(jù)庫刪除重復(fù)數(shù)據(jù)的方法只保留一條的文章就介紹到這了,更多相關(guān)mysql刪除重復(fù)數(shù)據(jù)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。