MySQL 消除重复行的一些方法

 更新时间:2017年05月20日 13:55:53   投稿:mdxy-dxy  
这篇文章主要介绍了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 

相关文章

  • mysql仿asp的数据库操作类

    mysql仿asp的数据库操作类

    本文通过实例代码给大家介绍了mysql仿asp的数据库操作类,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
    2008-04-04
  • Centos7下使用yum安装mysql数据库的详细教程(增强版)

    Centos7下使用yum安装mysql数据库的详细教程(增强版)

    这篇文章主要介绍了Centos7下使用yum安装mysql数据库的详细教程(增强版),非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-12-12
  • 在MySQL中使用mysqlbinlog flashback的简单教程

    在MySQL中使用mysqlbinlog flashback的简单教程

    这篇文章主要介绍了在MySQL中使用mysqlbinlog flashback的简单教程,可以很方便地恢复数据,作者还列出了使用时一些需要注意的地方,需要的朋友可以参考下
    2015-05-05
  • Mysql存在则修改不存在则新增的两种实现方法实例

    Mysql存在则修改不存在则新增的两种实现方法实例

    mysql语法支持如果数据存在则更新,不存在则插入,下面这篇文章主要给大家介绍了关于Mysql存在则修改不存在则新增的两种实现方法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • centOS安装mysql5.7详细教程

    centOS安装mysql5.7详细教程

    这篇文章主要为大家介绍了centOS安装mysql5.7详细教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • php 读取mysql数据库三种方法

    php 读取mysql数据库三种方法

    mysql 读取数据库三种方法,需要的朋友可以参考下。
    2009-11-11
  • 浅谈MySql update会锁定哪些范围的数据

    浅谈MySql update会锁定哪些范围的数据

    本文主要介绍了记录一下MySql update会锁定哪些范围的数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 一文带你了解MySQL基于规则的优化

    一文带你了解MySQL基于规则的优化

    MySQL依据一些规则,竭尽全力的把这些很糟糕的语句转换成某种可以比较高效执行的形式,这个过程也可以被称作查询重写,本章主要就是详细讲解下这些比较重要的重写规则,感兴趣的小伙伴可跟着小编一起来学习
    2023-05-05
  • mysql or走索引加索引及慢查询的作用

    mysql or走索引加索引及慢查询的作用

    这篇文章主要介绍了mysql or走索引加索引及慢查询的作用,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • MySQL计划任务(事件调度器) Event Scheduler介绍

    MySQL计划任务(事件调度器) Event Scheduler介绍

    MySQL5.1.x版本中引入了一项新特性EVENT,顾名思义就是事件、定时任务机制,在指定的时间单元内执行特定的任务,因此今后一些对数据定时性操作不再依赖外部程序,而直接使用数据库本身提供的功能
    2013-10-10

最新评论