转自51CTO URL:http://dadloveu.blog.51cto.com/715500/196309/
有关mysql删除重复记录的方法,我在网上看到很多文章,很多是照抄的,我自己按网上的方法实验了一下,没有一个sql语句就能解决的方法,不知道有没有高手可以出招。
我试验的过程如下:
mysql> select * from duplicate;
+----+-------+
| id | name |
+----+-------+
| 1 | wang |
| 2 | wang |
| 3 | wdang |
| 4 | wdang |
| 5 | wdand |
| 6 | wddda |
+----+-------+
6 rows in set (0.00 sec)
select * from duplicate where id in(select min(id) from duplicate group by name);
+----+-------+
| id | name |
+----+-------+
| 1 | wang |
| 3 | wdang |
| 5 | wdand |
| 6 | wddda |
+----+-------+
4 rows in set (0.01 sec)
mysql> delete from duplicate where id not in(select min(id) from duplicate group by name);
ERROR 1093 (HY000): You can't specify target table 'duplicate' for update in FROM clause
最后我用了笨办法,复制无重复记录到新表格,删除旧表格,然后重命名新表格为旧表名称。
mysql> select * from duplicate where id in(select min(id) from duplicate group by name);
+----+-------+
| id | name |
+----+-------+
| 1 | wang |
| 3 | wdang |
| 5 | wdand |
| 6 | wddda |
+----+-------+
4 rows in set (0.01 sec)
mysql> create table duplica select * from duplicate where id in(select min(id) from duplicate group by name);
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> drop table duplicate;
Query OK, 0 rows affected (0.01 sec)
mysql> alter table duplica rename to duplicate;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from duplicate;
+----+-------+
| id | name |
+----+-------+
| 1 | wang |
| 3 | wdang |
| 5 | wdand |
| 6 | wddda |
+----+-------+
4 rows in set (0.00 sec)
mysql> alter table duplicate modify id int(2) not null primary key auto_increment;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
后来想了一个语句搞定了:
mysql> use mysql
Database changed
mysql> select * from duplicate;
+----+-------+
| id | name |
+----+-------+
| 1 | wang |
| 3 | wdang |
| 5 | wdand |
| 6 | wddda |
| 2 | wang |
| 4 | wdang |
+----+-------+
6 rows in set (0.00 sec)
mysql> delete duplicate as a from duplicate as a,
-> (
-> select * from duplicate group by name having count(1)>1) as b
-> where a.name=b.name and a.id > b.id;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from duplicate;
+----+-------+
| id | name |
+----+-------+
| 1 | wang |
| 3 | wdang |
| 5 | wdand |
| 6 | wddda |
+----+-------+
4 rows in set (0.00 sec)
分享到:
相关推荐
MySQL删除重复记录方法。
删除重复记录 删除数据表中的重复记录,包括MySQL、SQL Server、Oracle等
查询及删除重复记录的SQL语句,虽然有点乱,但内容还是不错的。
本文主要给大家介绍了关于MySQL中查询、删除重复记录的方法,分享出来供大家参考学习,下面来看看详细的介绍: 查找所有重复标题的记录: select title,count(*) as count from user_table group by title having ...
mysql彻底理解删除重复记录sql脚本,只保留一条记录
本文实例讲述了mysql优化小技巧之去除重复项实现方法。分享给大家供大家参考,具体如下: 说到这个去重,脑仁不禁得一疼,尤其是出具量比较大的时候。毕竟咱不是专业的DB,所以嘞,只能自己弄一下适合自己去重方法了...
MySQL 去除重复数据实例详解 有两个意义上的重复记录,一是完全重复的记录,也即所有字段均都重复,二是部分字段重复的记录。对于第一种重复,比较容易解决,只需在查询语句中使用distinct关键字去重,几乎所有...
表结构: mysql> desc demo; +——-+——————+——+—–+———+—————-+ | Field | Type | Null | Key | Default | Extra | +——-+——————+——+—–+———+—————-+ | id | int(11) unsigned |...
通过存储过程,删除Mysql数据库表中的重复记录,只保留一条;
有些 MySQL 数据表中可能存在重复的记录,有些...让我们尝试一个实例:下表中无索引及主键,所以该表允许出现多条重复记录。 CREATE TABLE person_tbl ( first_name CHAR(20), last_name CHAR(20), sex CHAR(10) )
本篇文章是对mysql中的单表distinct、多表group by查询去除重复记录进行了详细的分析介绍,需要的朋友参考下
今天我们就来谈谈如何查找 MySQL 表中的重复数据以及如何删除这些重复的记录。 创建示例表 首先创建一个示例表 people 并生成一些数据: drop table if exists people; create table people ( id int auto_...
主要为大家详细介绍了MySQL处理重复数据的实现代码,如何防止数据表出现重复数据及如何删除数据表中的重复数据,感兴趣的小伙伴们可以参考一下
注:此处“重复”非完全重复,意为某字段数据重复 HZT表结构 ID int Title nvarchar(50) AddDate datetime 数据 一. 查找重复记录 1. 查找全部重复记录 Select * From 表 Where 重复字段 In (Select ... 删除重复记录
Mysql 查询可以删除的重复数据 select t1.* from employee t1 where (t1.emp_name) in (select t4.emp_name from (select t2.emp_name from employee t2 group by t2.emp_name having count(*)>1) t4) and t1.emp_...
MySQL 处理重复数据 有些 MySQL 数据表中可能存在重复的记录,有些...让我们尝试一个实例:下表中无索引及主键,所以该表允许出现多条重复记录。 CREATE TABLE person_tbl ( first_name CHAR(20), last_name CHAR