MySQL 數據庫中刪除重復數據的方法


 演示數據,僅供參考

查詢表結構:

mysql> desc test; +-------+------------------+------+-----+---------+----------------+
| 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 test order by id; +----+------------------------+
| id | site                   |
+----+------------------------+|  1 | http://www.baidu.com  |
|  2 | http://www.hao123.com        |
|  3 | http://www.huwei.com |
|  4 | http://www.baidu.com  |
|  5 | http://www.huwei.com |
+----+------------------------+
5 rows in set (0.00 sec)

當沒有創建表或創建索引權限的時候,如果你要刪除較舊的重復記錄,可以使用下面的語句:

mysql> delete from a
    -> using test as a, test as b
    -> where (a.id > b.id)
    -> and (a.site = b.site);
Query OK, 2 rows affected (0.12 sec)

mysql> select * from test order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.baidu.com  |
|  2 | http://www.hao123.com        |
|  3 | http://www.huwei.com |
+----+------------------------+
3 rows in set (0.00 sec)

 

如果你要刪除較新的重復記錄,可以使用下面的語句:

mysql> delete from a
    -> using test as a, test as b
    -> where (a.id < b.id)
    -> and (a.site = b.site);
Query OK, 2 rows affected (0.12 sec)

mysql> select * from test order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  2 | http://www.hao123.com        |
|  4 | http://www.baidu.com  |
|  5 | http://www.huwei.com |
+----+------------------------+
3 rows in set (0.00 sec)

你可以用下面的語句先確認將被刪除的重復記錄:

mysql> SELECT a.*
    -> FROM test a, test b
    -> WHERE a.id > b.id
    -> AND (a.site = b.site);
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.baidu.com  |
|  3 | http://www.huwei.com |
+----+------------------------+
2 rows in set (0.00 sec)

如果有創建索引的權限,在表上創建唯一鍵索引,可以用下面的方法:

mysql> alter ignore table test add unique index ukey (site);
Query OK, 5 rows affected (0.46 sec)
Records: 5  Duplicates: 2  Warnings: 0

mysql> select * from test order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.baidu.com  |
|  2 | http://www.hao123.com        |
|  3 | http://www.huwei.com |
+----+------------------------+
3 rows in set (0.00 sec)
 
         

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

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

如果有創建表的權限,創建一個新表,然后將原表中不重復的數據插入新表:

mysql> create table test_new as select * from test group by site;
Query OK, 3 rows affected (0.19 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
| test_new       |
+----------------+
2 rows in set (0.00 sec)

mysql> select * from test order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.baidu.com  |
|  2 | http://www.hao123.com        |
|  3 | http://www.huwei.com |
|  4 | http://www.baidu.com  |
|  5 | http://www.huwei.com |
+----+------------------------+
5 rows in set (0.00 sec)

mysql> select * from test_new order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.baidu.com  |
|  2 | http://www.hao123.com        |
|  3 | http://www.huwei.com |
+----+------------------------+
3 rows in set (0.00 sec)

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

mysql> rename table test to test_old, test_new to test;
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
| test_old       |
+----------------+
2 rows in set (0.00 sec)

mysql> select * from test order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.baidu.com  |
|  2 | http://www.hao123.com        |
|  3 | http://www.huwei.com |
+----+------------------------+
3 rows in set (0.00 sec)

注意:使用這種方式創建的表會丟失原表的索引信息!

mysql> desc test;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(11) unsigned | NO   |     | 0       |       |
| site  | varchar(100)     | NO   |     |         |       |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM