MySQL數(shù)據(jù)庫(kù)中刪除重復(fù)記錄的方法總結(jié)[推薦]
mysql> desc demo;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| site | varchar(100) | NO | MUL | | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
數(shù)據(jù):
mysql> select * from demo order by id;
+----+------------------------+
| id | site |
+----+------------------------+
| 1 | http://www.CodeBit.cn |
| 2 | http://YITU.org |
| 3 | http://www.ShuoWen.org |
| 4 | http://www.CodeBit.cn |
| 5 | http://www.ShuoWen.org |
+----+------------------------+
5 rows in set (0.00 sec)
當(dāng)沒(méi)有創(chuàng)建表或創(chuàng)建索引權(quán)限的時(shí)候,可以用下面的方法:
如果你要?jiǎng)h除較舊的重復(fù)記錄,可以使用下面的語(yǔ)句:
mysql> delete from a
-> using demo as a, demo as b
-> where (a.id > b.id)
-> and (a.site = b.site);
Query OK, 2 rows affected (0.12 sec)
mysql> select * from demo order by id;
+----+------------------------+
| id | site |
+----+------------------------+
| 1 | http://www.CodeBit.cn |
| 2 | http://YITU.org |
| 3 | http://www.ShuoWen.org |
+----+------------------------+
3 rows in set (0.00 sec)
如果你要?jiǎng)h除較新的重復(fù)記錄,可以使用下面的語(yǔ)句:
mysql> delete from a
-> using demo as a, demo as b
-> where (a.id < b.id)
-> and (a.site = b.site);
Query OK, 2 rows affected (0.12 sec)
mysql> select * from demo order by id;
+----+------------------------+
| id | site |
+----+------------------------+
| 2 | http://YITU.org |
| 4 | http://www.CodeBit.cn |
| 5 | http://www.ShuoWen.org |
+----+------------------------+
3 rows in set (0.00 sec)
你可以用下面的語(yǔ)句先確認(rèn)將被刪除的重復(fù)記錄:
mysql> SELECT a.*
-> FROM demo a, demo b
-> WHERE a.id > b.id
-> AND (a.site = b.site);
+----+------------------------+
| id | site |
+----+------------------------+
| 1 | http://www.CodeBit.cn |
| 3 | http://www.ShuoWen.org |
+----+------------------------+
2 rows in set (0.00 sec)
如果有創(chuàng)建索引的權(quán)限,可以用下面的方法:
在表上創(chuàng)建唯一鍵索引:
mysql> alter ignore table demo add unique index ukey (site); Query OK, 5 rows affected (0.46 sec) Records: 5 Duplicates: 2 Warnings: 0 mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.CodeBit.cn | | 2 | http://YITU.org | | 3 | http://www.ShuoWen.org | +----+------------------------+ 3 rows in set (0.00 sec)
重復(fù)記錄被刪除后,如果需要,可以刪除索引:
mysql> alter table demo drop index ukey; Query OK, 3 rows affected (0.37 sec) Records: 3 Duplicates: 0 Warnings: 0
如果有創(chuàng)建表的權(quán)限,可以用下面的方法:
創(chuàng)建一個(gè)新表,然后將原表中不重復(fù)的數(shù)據(jù)插入新表:
mysql> create table demo_new as select * from demo group by site; Query OK, 3 rows affected (0.19 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | demo | | demo_new | +----------------+ 2 rows in set (0.00 sec) mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.CodeBit.cn | | 2 | http://YITU.org | | 3 | http://www.ShuoWen.org | | 4 | http://www.CodeBit.cn | | 5 | http://www.ShuoWen.org | +----+------------------------+ 5 rows in set (0.00 sec) mysql> select * from demo_new order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.CodeBit.cn | | 2 | http://YITU.org | | 3 | http://www.ShuoWen.org | +----+------------------------+ 3 rows in set (0.00 sec)
然后將原表備份,將新表重命名為當(dāng)前表:
mysql> rename table demo to demo_old, demo_new to demo; Query OK, 0 rows affected (0.04 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | demo | | demo_old | +----------------+ 2 rows in set (0.00 sec) mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.CodeBit.cn | | 2 | http://YITU.org | | 3 | http://www.ShuoWen.org | +----+------------------------+ 3 rows in set (0.00 sec)
注意:使用這種方式創(chuàng)建的表會(huì)丟失原表的索引信息!
mysql> desc demo; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | id | int(11) unsigned | NO | | 0 | | | site | varchar(100) | NO | | | | +-------+------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
如果要保持和原表信息一致,你可以使用 show create table demo; 來(lái)查看原表的創(chuàng)建語(yǔ)句,然后使用原表的創(chuàng)建語(yǔ)句創(chuàng)建新表,接著使用 insert … select 語(yǔ)句插入數(shù)據(jù),再重命名表即可。
當(dāng)然,如果要避免重復(fù)記錄,最好的辦法還是不要插入重復(fù)數(shù)據(jù),可以參考本站另外一篇文章:MySQL 當(dāng)記錄不存在時(shí)插入
- sqlserver中重復(fù)數(shù)據(jù)值只取一條的sql語(yǔ)句
- sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法
- mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實(shí)例詳解
- MySQL根據(jù)某一個(gè)或者多個(gè)字段查找重復(fù)數(shù)據(jù)的sql語(yǔ)句
- mysql數(shù)據(jù)庫(kù)刪除重復(fù)數(shù)據(jù)只保留一條方法實(shí)例
- Mysql?刪除重復(fù)數(shù)據(jù)保留一條有效數(shù)據(jù)(最新推薦)
- oracle/mysql數(shù)據(jù)庫(kù)多條重復(fù)數(shù)據(jù)如何取最新的
相關(guān)文章
MySQL事務(wù)的四大特性以及并發(fā)事務(wù)問(wèn)題解讀
這篇文章主要介紹了MySQL事務(wù)的四大特性以及并發(fā)事務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09MySQL安裝常見報(bào)錯(cuò)處理方法總結(jié)大全
MySQL數(shù)據(jù)庫(kù)在安裝或卸載的過(guò)程中,常常會(huì)出現(xiàn)一些錯(cuò)誤,這是件讓我們頭疼的事,下面這篇文章主要給大家介紹了關(guān)于MySQL安裝常見報(bào)錯(cuò)處理方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07SQL實(shí)現(xiàn)LeetCode(196.刪除重復(fù)郵箱)
這篇文章主要介紹了SQL實(shí)現(xiàn)LeetCode(196.刪除重復(fù)郵箱),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08MySQL多實(shí)例的配置應(yīng)用實(shí)例場(chǎng)景
在一臺(tái)服務(wù)器上,運(yùn)行多個(gè)數(shù)據(jù)庫(kù)服務(wù),這些服務(wù)進(jìn)程通過(guò)不同的socket監(jiān)聽不同的服務(wù)端口來(lái)提供各自的服務(wù),這篇文章主要介紹了MySQL多實(shí)例的配置場(chǎng)景分析,需要的朋友可以參考下2021-12-12windows7下啟動(dòng)mysql服務(wù)出現(xiàn)服務(wù)名無(wú)效的原因及解決方法
這篇文章主要介紹了windows7下啟動(dòng)mysql服務(wù)出現(xiàn)服務(wù)名無(wú)效的原因及解決方法,需要的朋友可以參考下2014-06-06MySQL 8.0新特性 — 檢查性約束的使用簡(jiǎn)介
這篇文章主要介紹了MySQL 8.0新特性 — 檢查性約束的簡(jiǎn)單介紹,幫助大家更好的理解和學(xué)習(xí)使用MySQL數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2021-03-03