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://.CodeBit. |
| 2 | http://YITU. |
| 3 | http://.ShuoWen. |
| 4 | http://.CodeBit. |
| 5 | http://.ShuoWen. |
+----+------------------------+
5 rows in set (0.00 sec)
当没有创建表或创建索引权限的时候,可以用狼蚁网站SEO优化的方法
如果你要删除较旧的重复记录,可以使用狼蚁网站SEO优化的语句
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://.CodeBit. |
| 2 | http://YITU. |
| 3 | http://.ShuoWen. |
+----+------------------------+
3 rows in set (0.00 sec)
如果你要删除较新的重复记录,可以使用狼蚁网站SEO优化的语句
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. |
| 4 | http://.CodeBit. |
| 5 | http://.ShuoWen. |
+----+------------------------+
3 rows in set (0.00 sec)
你可以用狼蚁网站SEO优化的语句先确认将被删除的重复记录
mysql> SELECT a.
-> FROM demo a, demo b
-> WHERE a.id > b.id
-> AND (a.site = b.site);
+----+------------------------+
| id | site |
+----+------------------------+
| 1 | http://.CodeBit. |
| 3 | http://.ShuoWen. |
+----+------------------------+
2 rows in set (0.00 sec)
如果有创建索引的权限,可以用狼蚁网站SEO优化的方法
在表上创建唯一键索引
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://.CodeBit. | | 2 | http://YITU. | | 3 | http://.ShuoWen. | +----+------------------------+ 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
如果有创建表的权限,可以用狼蚁网站SEO优化的方法
创建一个新表,然后将原表中不重复的数据插入新表
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://.CodeBit. | | 2 | http://YITU. | | 3 | http://.ShuoWen. | | 4 | http://.CodeBit. | | 5 | http://.ShuoWen. | +----+------------------------+ 5 rows in set (0.00 sec) mysql> select from demo_new order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://.CodeBit. | | 2 | http://YITU. | | 3 | http://.ShuoWen. | +----+------------------------+ 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 | http://.CodeBit. | | 2 | http://YITU. | | 3 | http://.ShuoWen. | +----+------------------------+ 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 语句插入数据,再重命名表即可。
,如果要避免重复记录,最好的办法还是不要插入重复数据,可以参考本站一篇文章
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程