MySQL 消除重复行的一些方法
作者:mdxy-dxy 发布时间:2024-01-18 12:17:59
标签:MySQL,消除,重复行
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


猜你喜欢
- 前言字符串作为一种重要的Python基本数据类型,在数据处理中发挥着不可或缺的作用,如果对它的方法能够灵活使用,能够达到事半功倍的效果。下面
- reflow是个神奇的东西,之前Realazy说到过这个reflow,我摘出其中的重点:在CSS规范中有一个渲染对象的概念,通常用一个盒子(
- PDOStatement::executePDOStatement::execute — 执行一条预处理语句(PHP 5 >= 5.1
- 1、创建Django项目打开pycharm,新建Django项目,可以选择一个虚拟环境建完之后目录如下:2、创建应用,我这里命名为demo在
- 导语哈喽吖铁汁萌!今天这期就给大家介绍几个我用到的办公室自动化技巧,可以瞬速提高办公效率。有需要的可以往下滑了1、Word文档doc转doc
- 受杰森的《Almost Looks Like Work》启发,我来展示一些病毒传播模型。需要注意的是这个模型并不反映现实情况,因此不要误以为
- 关于oracle 优化的内容很多,概念庞杂,不过可以总结出一个大纲性的东西作为需要考虑的方向,然后再逐步细化。oracle优化按重要性需要考
- 如下所示:# u [32,30,200]# u_logits [400,32,30]q_j_400 = [] for j in range(
- 前言Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。由于 Tki
- MySQL 在处理 GROUP BY 和 DISTINCT 查询的方式在大多数情况下类似,事实上,在优化过程中有时候会把在这两种方式中转换。
- 什么是异常?异常是一个事件,其中一个程序,破坏程序的指令的正常流的执行过程中而发生的。一般情况下,当一个Python脚本遇到一些情况不能处理
- 一 Cookie因为HTTP协议是没有状态的,但很多情况下是需要一些信息的,比如在用户登陆后、再次访问网站时,没法判断用户是否登陆过。于是就
- Request.ServerVariables("Url") 返回服务器地址Request.ServerVariable
- 第一个:神奇的字典键some_dict = {}some_dict[5.5] = "Ruby"some_dict[5.0
- 全文索引在 MySQL 中是一个 FULLTEXT 类型索引。FULLTEXT 索引用于 MyISAM 表,可以在 CREATE TABLE
- 引言caffe是C++语言写的,可能很多人不太熟悉,因此想用更简单的脚本语言来实现。caffe提供matlab接口和python接口,这两种
- numpy.linalg.norm函数的使用1、linalg = linear(线性)+ algebra(代数),norm则表示范数。首先需
- 本文实例讲述了Python selenium的基本使用方法。分享给大家供大家参考,具体如下:selenium是一个web自动化测试工具,se
- SQL Server 2005相对于SQL Server 2000做了很大的改进,许些新特性是非常实用的。本文中将通过几个具体示例进行详细的
- 本文实例为大家分享了Bootstrap进度条的具体实现代码,供大家参考,具体内容如下<!doctype html><htm