mysql中删除重复记录sql语句

删除重复记录方法一:

1. 新建一个临时表

 代码如下 复制代码

create table tmp as select * from youtable group by  name(name为不希望有重复的列)

2. 删除原来的表

 代码如下 复制代码

drop table youtable

3. 重命名表

 代码如下 复制代码

alter table tmp rename youtable

但是这个方法有个问题,由临时表转变过来的最终表,其表结构会和原来的不一致,需要手工更改。这个问题,待解决。
删除重复记录方法二:

1. 新建一个临时表

 代码如下 复制代码

CREATE TABLE tmp AS SELECT * FROM youtable GROUP BY name(name为不希望有重复的列)

2. 清空原来的表

 代码如下 复制代码

TRUNCATE TABLE youtable

3. 把临时表插入到youtable

 代码如下 复制代码

INSERT INTO tablename SELECT  * FROM temp

4. 删除临时表

 代码如下 复制代码

DROP TABLE  temp

删除重复记录方法三:

 代码如下 复制代码

delete table where ID not in(select min(ID) from table group by name(name:重复的字段))

删除重复记录方法四:

具体实现如下:

 代码如下 复制代码

Table         Create Table                                           
------------  --------------------------------------------------------
users_groups  CREATE TABLE `users_groups` (                          
                `id` int(10) unsigned NOT NULL AUTO_INCREMENT,       
                `uid` int(11) NOT NULL,                              
                `gid` int(11) NOT NULL,                              
                PRIMARY KEY (`id`)                                   
              ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 

users_groups.txt内容:

1,11,502
2,107,502
3,100,503
4,110,501
5,112,501
6,104,502
7,100,502
8,100,501
9,102,501
10,104,502
11,100,502
12,100,501
13,102,501
14,110,501

mysql> load data infile 'c:\users_groups.txt' into table users_groups fields
terminated by ',' lines terminated by 'n';
Query OK, 14 rows affected (0.05 sec)
Records: 14  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from users_groups;

query result(14 records)

id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
7 100 502
8 100 501
9 102 501
10 104 502
11 100 502
12 100 501
13 102 501
14 110 501
14 rows in set (0.00 sec)

根据一位兄弟的建议修改。

 代码如下 复制代码

mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) >= 1;
Query OK, 7 rows affected (0.11 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> truncate table users_groups;
Query OK, 14 rows affected (0.03 sec)

mysql> insert into users_groups select * from tmp_wrap;
Query OK, 7 rows affected (0.03 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> select * from users_groups;

query result(7 records)
id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
9 102 501

mysql> drop table tmp_wrap;
Query OK, 0 rows affected (0.05 sec)

2、还有一个很精简的办法。

查找重复的,并且除掉最小的那个。

 代码如下 复制代码

delete users_groups as a from users_groups as a,
(
select *,min(id) from users_groups group by uid having count(1) > 1
) as b
 where a.uid = b.uid and a.id > b.id;
(7 row(s)affected)
(0 ms taken)
 
query result(7 records)
id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
9 102 501

3、现在来看一下这两个办法的效率。
运行一下以下SQL 语句

 代码如下 复制代码

create index f_uid on users_groups(uid);
explain select * from users_groups group by uid having count(1) > 1 union all
select * from users_groups group by uid having count(1) = 1;
explain select * from  users_groups as a,
(
select *,min(id) from users_groups group by uid having count(1) > 1
) as b
 where a.uid = b.uid and a.id > b.id;
query result(3 records)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY users_groups index (NULL) f_uid 4 (NULL) 14  
2 UNION users_groups index (NULL) f_uid 4 (NULL) 14  
(NULL) UNION RESULT <union1,2> ALL (NULL) (NULL) (NULL) (NULL) (NULL)  

 
query result(3 records)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 4  
1 PRIMARY a ref PRIMARY,f_uid f_uid 4 b.uid 1 Using where
2 DERIVED users_groups index (NULL) f_uid 4 (NULL) 14  

 
 

很明显的第二个比第一个扫描的函数要少。

当没有创建表或创建索引权限的时候

创建一个新表,然后将原表中不重复的数据插入新表:

 代码如下 复制代码

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://www.111cn.net  |
|  2 | http://111cn.net        |
|  3 | http://www.111cn.net |
|  4 | http://www.111cn.net  |
|  5 | http://www.111cn.net |
+----+------------------------+
5 rows in set (0.00 sec)
 
mysql> select * from demo_new order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.111cn.net  |
|  2 | http://111cn.net        |
|  3 | http://www.111cn.net |
+----+------------------------+
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://www.111cn.net  |
|  2 | http://111cn.net        |
|  3 | http://www.111cn.net |
+----+------------------------+
3 rows in set (0.00 sec)

时间: 2024-09-10 20:14:39

mysql中删除重复记录sql语句的相关文章

删除重复记录 sql语句

删除重复记录 sql语句 本教程为你提供了二款关于删除重复记录的sql语句代码.一个利用where in查询重复记录再实现删除,一个是用inner join来实例删除重记录. */ //方法一 delete from tablea where title in (select a.title from tablea a join tableb b on a.title = b.title) //方法二 delete a.* from tablea a inner join tableb b on

Oracle 查询与删除表中的重复记录sql语句

方法:  代码如下 复制代码 group by  XX having count(*)>1,rowid,distinct,temporary table,procedure 下面语句可以查询出那些数据是重复的:  代码如下 复制代码 select 字段1,字段2,count(*) from 表名 group by 字段1,字段2 having count(*) > 1 将上面的>号改为=号就可以查询出没有重复的数据了. 想要删除这些重复的数据,可以使用下面语句进行删除  代码如下 复制代

MySQL中删除重复数据的简单方法_Mysql

MYSQL里有五百万数据,但大多是重复的,真实的就180万,于是想怎样把这些重复的数据搞出来,在网上找了一圈,好多是用NOT IN这样的代码,这样效率很低,自己琢磨组合了一下,找到一个高效的处理方式,用这个方式,五百万数据,十来分钟就全部去除重复了,请各位参考. 第一步:从500万数据表data_content_152里提取出不重复的字段SFZHM对应的ID字段到TMP3表 create table tmp3 as select min(id) as col1 from data_content

删除重复数据sql语句

删除重复数据sql语句 方法一 假设有重复的字段为name,address,要求得到这两个字段唯一的结果集 select identity(int,1,1) as autoid, * into #tmp from tablename select min(autoid) as autoid into #tmp2 from #tmp group by name,autoid select * from #tmp where autoid in(select autoid from #tmp2) 方

异常-mysql 中多次执行sql语句,提示too many connections

问题描述 mysql 中多次执行sql语句,提示too many connections 解决方案 mysql Too many connectionsmysql: Too many connectionsmysql Too many connections 解决方案二: 看代码是结合上下文.第一份有一个关闭流,是否正确.这是一个jdbc操作mysql,参考一下吧:http://blog.csdn.net/qq_19558705/article/details/49947317 解决方案三: 为

SQL Server中删除重复记录的SQL语句

方法:  代码如下 复制代码            select distinct * into #tmp from tablename  drop table tablename  select * into tablename from #tmp drop table # tmp 常有时候遇到需要删除SQL Server中的重复记录,这里有一些常用的删除重复记录的SQL, 最常用的 T-SQL 语句:  代码如下 复制代码 DELETE FROM [dbo].[myTable] WHERE

MySQL 数据库中删除重复记录方法总结

MYSQL数据库中,经常会遇到重复记录的情况,那么就需要SQL删除重复记录,下面为您列举了四种删除重复记录的方式,用于不同的情况,希望对您有所帮助. 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断  代码如下 复制代码 select * from people  where peopleId in (select   peopleId from   people group by   peopleId having count(peopleId) > 1)    

在SQL中删除重复记录(多种方法)

重复|重复记录  学习sql有一段时间了,发现在我建了一个用来测试的表(没有建索引)中出现了许多的重复记录.后来总结了一些删除重复记录的方法,在Oracle中,可以通过唯一rowid实现删除重复记录:还可以建临时表来实现...这个只提到其中的几种简单实用的方法,希望可以和大家分享(以表employee为例). SQL> desc employee  Name                                      Null?    Type ------------------

在SQL中删除重复记录的多种方法

学习sql有一段时间了,发现在我建了一个用来测试的表(没有建索引)中出现了许多的重复记录.后来总结了一些删除重复记录的方法,在Oracle中,可以通过唯一rowid实现删除重复记录:还可以建临时表来实现...这个只提到其中的几种简单实用的方法,希望可以和大家分享(以表employee为例). SQL> desc employee Name Null? Type ----------------------------------------- -------- -----------------