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)注腳本之家其它相關(guān)文章!
相關(guān)文章
MySQL數(shù)據(jù)庫高級(jí)數(shù)據(jù)操作之新增數(shù)據(jù)
這篇文章主要介紹了MySQL數(shù)據(jù)庫高級(jí)數(shù)據(jù)操作之新增數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
查看修改mysql編碼方式讓它支持中文(gbk或者utf8)
MySQL的默認(rèn)編碼是Latin1,不支持中文,要支持中文需要把數(shù)據(jù)庫的默認(rèn)編碼修改為gbk或者utf8,真的是很麻煩啊,不過本文提供了詳細(xì)的修改教程,感興趣的你可不要走開啊,希望本文對(duì)你有所幫助2013-01-01
MySQL使用正則表達(dá)式來更好地控制數(shù)據(jù)過濾
MySQL中的正則表達(dá)式是一種強(qiáng)大的數(shù)據(jù)過濾工具,它允許用戶以靈活的方式匹配和搜索文本數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于MySQL使用正則表達(dá)式來更好地控制數(shù)據(jù)過濾的相關(guān)資料,需要的朋友可以參考下2024-08-08
MySQL數(shù)據(jù)庫MyISAM存儲(chǔ)引擎轉(zhuǎn)為Innodb的方法
mysql數(shù)據(jù)庫存儲(chǔ)引擎為MyISAM的時(shí)候,在大訪問量的情況下數(shù)據(jù)表有可能會(huì)出現(xiàn)被鎖的情況,這就會(huì)導(dǎo)致用戶連接網(wǎng)站時(shí)超時(shí)而返回502,此時(shí)就需要MySQL數(shù)據(jù)庫MyISAM存儲(chǔ)引擎轉(zhuǎn)為Innodb,這篇文章主要介紹了MySQL數(shù)據(jù)庫MyISAM存儲(chǔ)引擎轉(zhuǎn)為Innodb的方法,需要的朋友可以參考下2014-06-06
MySQL全局遍歷替換特征字符串的實(shí)現(xiàn)方法
本文主要介紹了MySQL全局遍歷替換特征字符串的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

