MySQL資料庫中刪除重複記錄的方法總結[推薦]

dav2100發表於2021-09-09

           表結構:
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)

資料:
mysql> select * from demo order by id;
+----+------------------------+
| id | site |
+----+------------------------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
+----+------------------------+
5 rows in set (0.00 sec)

當沒有建立表或建立索引許可權的時候,可以用下面的方法:

如果你要刪除較舊的重複記錄,可以使用下面的語句:
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 | |
| 2 | |
| 3 | |
+----+------------------------+
3 rows in set (0.00 sec)

如果你要刪除較新的重複記錄,可以使用下面的語句:
mysql> delete from a
-> using demo as a, demo as b
-> where (a.id -> and (a.site = b.site);
Query OK, 2 rows affected (0.12 sec)

mysql> select * from demo order by id;
+----+------------------------+
| id | site |
+----+------------------------+
| 2 | |
| 4 | |
| 5 | |
+----+------------------------+
3 rows in set (0.00 sec)

你可以用下面的語句先確認將被刪除的重複記錄:
mysql> SELECT a.*
-> FROM demo a, demo b
-> WHERE a.id > b.id
-> AND (a.site = b.site);
+----+------------------------+
| id | site |
+----+------------------------+
| 1 | |
| 3 | |
+----+------------------------+
2 rows in set (0.00 sec)

如果有建立索引的許可權,可以用下面的方法:

在表上建立唯一鍵索引:

?


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 | | | 2 | | | 3 | | +----+------------------------+ 3 rows in set (0.00 sec)

重複記錄被刪除後,如果需要,可以刪除索引:

?


mysql> alter table demo drop index ukey; Query OK, 3 rows affected (0.37 sec) Records: 3 Duplicates: 0 Warnings: 0

如果有建立表的許可權,可以用下面的方法:

建立一個新表,然後將原表中不重複的資料插入新表:

?


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 | | | 2 | | | 3 | | | 4 | | | 5 | | +----+------------------------+ 5 rows in set (0.00 sec)  mysql> select * from demo_new order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | | | 2 | | | 3 | | +----+------------------------+ 3 rows in set (0.00 sec)

然後將原表備份,將新表重新命名為當前表:

?


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 | | | 2 | | | 3 | | +----+------------------------+ 3 rows in set (0.00 sec)

注意:使用這種方式建立的表會丟失原表的索引資訊!

?


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; 來檢視原表的建立語句,然後使用原表的建立語句建立新表,接著使用 insert … select 語句插入資料,再重新命名錶即可。

當然,如果要避免重複記錄,最好的辦法還是不要插入重複資料,可以參考本站另外一篇文章:MySQL 當記錄不存在時插入

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4479/viewspace-2803848/,如需轉載,請註明出處,否則將追究法律責任。

相關文章