MySQL 消除重复行的一些方法
作者:mdxy-dxy 时间:2024-01-18 12:17:59
sql语句
/*
MySQL 消除重复行的一些方法
---Chu Minfei
---2010-08-12 22:49:44.660
--引用转载请注明出处:http://blog.csdn.NET/feixianxxx
*/
----------------全部字段重复------------------------
--1使用表替换来删除重复项
create table test_1(id int,value int);
insert test_1 select 1,2 union all select 1,2 union all select 2,3;
--建立一个和源表结构一样的空的临时表
create table tmp like test_1;
--向临时表插入不重复的记录
insert tmp select distinct * from test_1;
--删除原表
drop table test_1;
--更改临时表名为目标表
rename table tmp to test_1;
--显示
mysql> select * from test_1;
+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
| 2 | 3 |
+------+-------+
--2.添加auto_increment属性列(这个方法只能用于MyISAM或者BDB引擎的表)
create table test_1(id int,value int) engine=MyISAM;
insert test_1 select 1,2 union all select 1,2 union all select 2,3;
alter table test_1 add id2 int not null auto_increment,
add primary key(id,value,id2);
select * from test_1;
+----+-------+-----+
| id | value | id2 |
+----+-------+-----+
| 1 | 2 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 1 |
+----+-------+-----+
delete from test_1 where id2<>1;
alter table test_1 drop id2;
select * from test_1;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 3 |
+----+-------+
-------------------部分字段重复---------------------
--1.加索引的方式
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
Alter IGNORE table test_2 add primary key(id);
select * from test_2;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 3 |
+----+-------+
我们可以看到 1 3 这条记录消失了
我们这里也可以使用Unique约束 因为有可能列中有NULL值,但是这里NULL就可以多个了..
--2.联合表删除
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
delete A from test_2 a join (select MAX(value) as v ,ID from test_2 group by id) b
on a.id=b.id and a.value<>b.v;
select * from test_2;
+------+-------+
| id | value |
+------+-------+
| 1 | 3 |
| 2 | 3 |
+------+-------+
--3.使用Increment_auto也可以就是上面全部字段去重的第二个方法
--4.容易错误的方法
--有些朋友可能会想到子查询的方法,我们来试验一下
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
delete a from test_2 a where exists(select * from test_2 where a.id=id and a.value<value);
/*ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause*/
目前,您不能从一个表中删除,同时又在子查询中从同一个表中选择。
------------------删除特定重复行--------------
--主要通过order by +limit 或者直接limit
create table test_3(id int,value int);
insert test_3 select 1,2 union all select 1,3 union all select 1,4 union all select 2,3;
--这是要保留ID=1 value最小的那个记录,删除其他id为的记录
delete from test_3 where id=1 order by value desc limit 2;
select * from test_3;
+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
| 2 | 3 |
+------+-------+
如果你只想删除任意的记录 保留一条 就可以去掉order by
标签:MySQL,消除,重复行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python机器学习pytorch交叉熵损失函数的深刻理解
2021-12-11 06:09:40
![](https://img.aspxhome.com/file/2023/3/103743_0s.png)
选项卡动态增删的效果(内嵌框架)
2008-05-22 12:59:00
![](https://img.aspxhome.com/file/UploadPic/20085/22/asgard_cardprogram_62s.jpg)
python监控nginx端口和进程状态
2023-08-25 16:15:13
Python matplotlib可视化绘图详解
2021-11-11 12:03:26
![](https://img.aspxhome.com/file/2023/0/78480_0s.png)
Python利用3D引擎制作一个3D迷宫游戏
2021-02-18 21:17:54
![](https://img.aspxhome.com/file/2023/8/75368_0s.jpg)
Python实现连点器的示例代码
2023-04-17 00:11:29
![](https://img.aspxhome.com/file/2023/8/124138_0s.png)
在Mac OS上安装Go语言编译器的方法
2024-05-22 17:48:40
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
2023-11-16 04:45:22
![](https://img.aspxhome.com/file/2023/0/63910_0s.png)
Python 高级专用类方法的实例详解
2023-10-11 14:13:52
在ASP与ASP.NET之间共享对话状态(2)
2008-09-02 12:21:00
在页面中动态载入外部javascript
2007-09-30 12:52:00
js打开新窗口方法代码收集
2007-09-05 19:20:00
多列复合索引的使用 绕过微软sql server的一个缺陷
2012-08-21 10:37:36
如何利用Python将html转为pdf、word文件
2022-07-27 10:36:55
![](https://img.aspxhome.com/file/2023/5/76085_0s.png)
轻松掌握python设计模式之策略模式
2022-01-19 00:17:15
![](https://img.aspxhome.com/file/2023/3/67843_0s.jpg)
Go语言实现配置热加载的方法分享
2024-02-07 04:02:47
![](https://img.aspxhome.com/file/2023/5/114815_0s.webp)
Python中处理无效数据的详细教程
2021-11-18 06:11:07
![](https://img.aspxhome.com/file/2023/6/83736_0s.png)
python实现的爬取电影下载链接功能示例
2023-02-06 20:30:47
MySQL中IO问题的深入分析与优化
2024-01-19 14:06:48
![](https://img.aspxhome.com/file/2023/4/71774_0s.png)
解析pandas apply() 函数用法(推荐)
2022-09-02 15:10:09