mysql删除重复记录sql语句
发布:smiling 来源: PHP粉丝网 添加日期:2014-10-08 16:00:25 浏览: 评论:0
我们经常会碰到需要删除数据表中重复记录,下面我来总结了几种能删除重复记录并助相对来讲效率是非常不错的,有需要了解的朋友可进入参考.
创建一个表用来存放,要删除的记录的id信息,代码如下:
- CREATE TABLE `tmp_ids` (
- `id` int(11),
- `name` char(20)
- ) ENGINE=MyISAM;
如果要删除的记录不多的话,可以把这个表创建成内存表形式,代码如下:
- CREATE TABLE `tmp_ids` (
- `id` int(11),
- `name` char(20)
- ) ENGINE=HEAP;
然后在test表中删除重复记录,代码如下:
- insert into tmp_ids select min(id),name from test group by name having count(*)>1 order by null;
- delete a.* from test a,tmp_ids b where b.name=a.name and a.id>b.id;
- truncate table tmp_ids;
方法二,复制无重复记录到新表格,删除旧表格,然后重命名新表格为旧表名称,代码如下 :
- 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)
- --phpfensi.com
- 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)
保留ID最小的记录.
第1种,代码如下:
- delete from %s where goodsurl in (select goodsrul as gurl1 from %s
- #group by grul1 having count(gurl1)>1)rs1 and id not in (select min(id)as id2 from %s
- #group by goodsurl having count(goodsurl)>1)rs2"%(a,a,a)
第2种,这种方法不使用子集,但是我不知道怎么把rs1,rs2里面的元组嵌入到SQL语句中,代码如下:
- exeSql = "select min(id) from %s group by goodsurl havingcount(goodsurl)>1)"%(a,) --phpfensi.com
- cur.execute(exeSql)
- rs1 = cur.fetchall()
- exeSql = "select goodsurl from %s group by goodsurl havingcount(goodsurl)>1"%(a,)
- cur.execute(exeSql)
- rs2 = cur.fetchall()
- exeSql = "delete from %s where goodsurl in %s and id not in %"%(a,rs2,rs1)
- cur.execute(exeSql)
Tags: mysql删除记录 mysql重复记录
相关文章
- ·mysql delete删除记录数据库空间不减少问题解决方法(2014-09-23)
- ·mysql中truncate删除记录恢复笔记(2014-09-24)
- ·mysql中删除记录TRUNCATE与DELETE区别(2014-09-24)
- ·MySQL删除记录Delete 与Truncate 语句介绍(2014-10-03)
- ·mysql删除重复数据记录sql语句总结(2014-10-05)
- ·MySQL 数据库中删除重复记录方法总结(2014-10-12)
- ·mysql重复记录取最后一条记录方法(2014-10-03)
- ·MySQL查询重复记录sql语句(2014-10-08)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)