MySQL 數(shù)據(jù)查重、去重的實(shí)現(xiàn)語句
有一個表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ù)記錄的各個記錄組中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ù)記錄的各個記錄組中id最大的記錄
select * from user where id in (select max(id) from user group by nick_name,password where having count(nick_name)>1);
查出各個重復(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. 快速解決mysql導(dǎo)數(shù)據(jù)時,格式不對、導(dǎo)入慢、丟數(shù)據(jù)的問題2. Mysql入門系列:在MYSQL結(jié)果集上執(zhí)行計(jì)算3. 巧用SQL語言在ACCESS數(shù)據(jù)庫中批量替換內(nèi)容4. navicat for mysql創(chuàng)建數(shù)據(jù)庫的方法5. 數(shù)據(jù)庫Oracle9i的企業(yè)管理器簡介6. MySQL MyISAM 與InnoDB 的區(qū)別7. MYSQL技巧:為現(xiàn)有字段添加自增屬性8. 如何遠(yuǎn)程調(diào)用ACCESS數(shù)據(jù)庫9. 初識SQLITE3數(shù)據(jù)庫10. 如何安裝MySQL 壓縮包

網(wǎng)公網(wǎng)安備