MySQL 數(shù)據(jù)查重、去重的實(shí)現(xiàn)語句
有一個(gè)表user,字段分別有id、nick_name、password、email、phone。
一、單字段(nick_name)
查出所有有重復(fù)記錄的所有記錄
select * from user where nick_name in (select nick_name from user group by nick_name having count(nick_name)>1);
查出有重復(fù)記錄的各個(gè)記錄組中id最大的記錄
select * from user where id in (select max(id) from user group by nick_name having count(nick_name)>1);
查出多余的記錄,不查出id最小的記錄
select * from user where nick_name in (select nick_name from user group by nick_name having count(nick_name)>1) and id not in (select min(id) from user group by nick_name having count(nick_name)>1);
刪除多余的重復(fù)記錄,只保留id最小的記錄
delete from user where nick_name in (select nick_name from (select nick_name from user group by nick_name having count(nick_name)>1) as tmp1) and id not in (select id from (select min(id) from user group by nick_name having count(nick_name)>1) as tmp2);
二、多字段(nick_name,password)
查出所有有重復(fù)記錄的記錄
select * from user where (nick_name,password) in (select nick_name,password from user group by nick_name,password where having count(nick_name)>1);
查出有重復(fù)記錄的各個(gè)記錄組中id最大的記錄
select * from user where id in (select max(id) from user group by nick_name,password where having count(nick_name)>1);
查出各個(gè)重復(fù)記錄組中多余的記錄數(shù)據(jù),不查出id最小的一條
select * from user where (nick_name,password) in (select nick_name,password from user group by nick_name,password having count(nick_name)>1) and id not in (select min(id) from user group by nick_name,password having count(nick_name)>1);
刪除多余的重復(fù)記錄,只保留id最小的記錄
delete from user where (nick_name,password) in (select nick_name,password from (select nick_name,password from user group by nick_name,password having count(nick_name)>1) as tmp1) and id not in (select id from (select min(id) id from user group by nick_name,password having count(nick_name)>1) as tmp2);
以上就是MySQL 數(shù)據(jù)查重、去重的實(shí)現(xiàn)語句的詳細(xì)內(nèi)容,更多關(guān)于MySQL 數(shù)據(jù)查重、去重的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Window7安裝MariaDB數(shù)據(jù)庫及系統(tǒng)初始化操作分析2. SQL案例學(xué)習(xí)之字符串的合并與拆分方法總結(jié)3. MariaDB數(shù)據(jù)庫的外鍵約束實(shí)例詳解4. SQLite 性能優(yōu)化實(shí)例分享5. SQL Server一個(gè)字符串拆分多行顯示或者多行數(shù)據(jù)合并成一個(gè)字符串6. access不能打開注冊表關(guān)鍵字錯(cuò)誤處理方法(80004005錯(cuò)誤)7. Centos7 下mysql重新啟動(dòng)MariaDB篇8. centos 7安裝mysql5.5和安裝 mariadb使用的命令9. MariaDB性能調(diào)優(yōu)工具mytop的使用詳解10. MariaDB的安裝與配置教程
