MySQL 數(shù)據(jù)查重、去重的實現(xiàn)語句
有一個表user,字段分別有id、nick_name、password、email、phone。
一、單字段(nick_name)
查出所有有重復記錄的所有記錄
select * from user where nick_name in (select nick_name from user group by nick_name having count(nick_name)>1);
查出有重復記錄的各個記錄組中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);
刪除多余的重復記錄,只保留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)
查出所有有重復記錄的記錄
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);
查出有重復記錄的各個記錄組中id最大的記錄
select * from user where id in (select max(id) from user group by nick_name,password where having count(nick_name)>1);
查出各個重復記錄組中多余的記錄數(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);
刪除多余的重復記錄,只保留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ù)查重、去重的實現(xiàn)語句的詳細內(nèi)容,更多關于MySQL 數(shù)據(jù)查重、去重的資料請關注腳本之家其它相關文章!
相關文章
MySQL數(shù)據(jù)庫高級數(shù)據(jù)操作之新增數(shù)據(jù)
這篇文章主要介紹了MySQL數(shù)據(jù)庫高級數(shù)據(jù)操作之新增數(shù)據(jù),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
查看修改mysql編碼方式讓它支持中文(gbk或者utf8)
MySQL的默認編碼是Latin1,不支持中文,要支持中文需要把數(shù)據(jù)庫的默認編碼修改為gbk或者utf8,真的是很麻煩啊,不過本文提供了詳細的修改教程,感興趣的你可不要走開啊,希望本文對你有所幫助2013-01-01
MySQL使用正則表達式來更好地控制數(shù)據(jù)過濾
MySQL中的正則表達式是一種強大的數(shù)據(jù)過濾工具,它允許用戶以靈活的方式匹配和搜索文本數(shù)據(jù),這篇文章主要給大家介紹了關于MySQL使用正則表達式來更好地控制數(shù)據(jù)過濾的相關資料,需要的朋友可以參考下2024-08-08
MySQL數(shù)據(jù)庫MyISAM存儲引擎轉為Innodb的方法
mysql數(shù)據(jù)庫存儲引擎為MyISAM的時候,在大訪問量的情況下數(shù)據(jù)表有可能會出現(xiàn)被鎖的情況,這就會導致用戶連接網(wǎng)站時超時而返回502,此時就需要MySQL數(shù)據(jù)庫MyISAM存儲引擎轉為Innodb,這篇文章主要介紹了MySQL數(shù)據(jù)庫MyISAM存儲引擎轉為Innodb的方法,需要的朋友可以參考下2014-06-06

