服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|

服务器之家 - 数据库 - Mysql - MySQL数据库中删除重复记录的方法总结[推荐]

MySQL数据库中删除重复记录的方法总结[推荐]

2019-11-27 16:54MYSQL教程网 Mysql

MySQL数据库中删除重复记录的方法总结[推荐]

表结构:
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 | 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> 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)

如果你要删除较新的重复记录,可以使用下面的语句:
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)

你可以用下面的语句先确认将被删除的重复记录:
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)

如果有创建索引的权限,可以用下面的方法:

在表上创建唯一键索引:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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)

重复记录被删除后,如果需要,可以删除索引:

?

1

2

3

mysql> alter table demo drop index ukey;

Query OK, 3 rows affected (0.37 sec)

Records: 3 Duplicates: 0 Warnings: 0

如果有创建表的权限,可以用下面的方法:

创建一个新表,然后将原表中不重复的数据插入新表:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

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)

然后将原表备份,将新表重命名为当前表:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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)

注意:使用这种方式创建的表会丢失原表的索引信息!

?

1

2

3

4

5

6

7

8

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 当记录不存在时插入

延伸 · 阅读

精彩推荐
  • MysqlMYSQL 数据库导入导出命令

    MYSQL 数据库导入导出命令

    在不同操作系统或MySQL版本情况下,直接拷贝文件的方法可能会有不兼容的情况发生。所以一般推荐用SQL脚本形式导入。下面分别介绍两种方法。 ...

    mysql技术网1442019-11-11
  • MysqlMySQL新手入门指南--快速参考

    MySQL新手入门指南--快速参考

    以下是一些重要的SQL快速参考,有关SQL的语法和在标准SQL上增加的特性,请查询MySQL手册。 ...

    mysql技术网3862019-10-15
  • MysqlMySQL组合索引与最左匹配原则详解

    MySQL组合索引与最左匹配原则详解

    这篇文章主要给大家介绍了关于MySQL组合索引与最左匹配原则的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Mysql具有一定的参考学习...

    Wolf、Heart1452019-06-01
  • MysqlMysql如何查询某条记录在分页的第几页详析

    Mysql如何查询某条记录在分页的第几页详析

    查询是我们日常工作中经常会遇到的一个功能,下面这篇文章主要给大家介绍了关于Mysql如何查询某条记录在分页的第几页的相关资料,文中通过示例代码...

    网络2032019-06-15
  • Mysql在同一台机器上运行多个 MySQL 服务

    在同一台机器上运行多个 MySQL 服务

    在Mysql中有一mysqld_multi命令,可用于在一台物理服务器运行多个Mysql服务,今天参考一些文档,亲自测试并通过,真高兴,现将操作过程共享给大家!...

    MySQL教程网2052019-10-15
  • MysqlMysql 数据库访问类

    Mysql 数据库访问类

    Mysql数据库访问类 实现代码,对于想学习mysql操作类的朋友值得一看 ...

    mysql教程网2352019-10-25
  • Mysql浅谈mysql使用limit分页优化方案的实现

    浅谈mysql使用limit分页优化方案的实现

    在mysql中limit可以实现快速分页,但是如果数据到了几百万时我们的limit必须优化才能有效的合理的实现分页了,否则可能卡死你的服务器哦。感兴趣的可以...

    爱情小傻蛋4592019-06-12
  • MysqlMySQL数据库远程连接开启方法

    MySQL数据库远程连接开启方法

    有时候需要远程连接mysql数据库,默认是不可以的,大家可以参考下面的方法,解决下。 ...

    mysql教程网2232019-11-10