mysql 数据库超强删除重复数据语句
发布:smiling 来源: PHP粉丝网 添加日期:2014-10-02 16:55:47 浏览: 评论:0
在mysql中删除重复数据的方法各千种,但我百度了N种删除重复数据方法,个人测试小数据量还没有问题,大数据量就卡死了,下面我来介绍超强删除重复数据语句实例,各位朋友有兴趣可参考.
月小升今天遇到的问题是students这个表有md这个字段重复,看看如何处理吧,代码如下:
- select * from students
- where md in (select md from students group by md having count(md) > 1) order by md
注明,这个被group的字段,请索引,否则很慢,代码如下:
- delete from students
- where md in (select md from students group by md having count(md) > 1)
- and id not in (select min(id) from students group by md having count(md )>1)
这个语句在mysql下会报错:#1093 – You can’t specify target table ‘students’ for update in FROM clause
原因是好像mysql不准许我们进行联合删除内有条件语句指向自己的表,策略是使用临时表,存储那些要删除的ID,代码如下:
- create table tmp (id int);
- insert into tmp (id) select id from students
- where md in (select md from students group by md having count(md) > 1)
- and id not in (select min(id) from students group by md having count(md )>1); --phpfensi.com
- delete from students where id in (select id from tmp);
得出会被删除的数据,代码如下:
- select * from students
- where md in (select md from students group by md having count(md) > 1)
- and id not in (select min(id) from students group by md having count(md )>1)
得出过滤后的数据,不删除的数据,如果不用删除,此sql语句可以用来显示唯一数据,代码如下:
- select * from students
- where
- id in (select min(id) from students group by md having count(md )>1)
Tags: mysql删除重复数据
相关文章
- ·Mysql中查找并删除重复数据的方法(2014-09-25)
- ·Mysql中如何查找并删除重复数据(2014-09-28)
- ·MySQL取出表中排序数据且不重复 删除重复数据(2014-10-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)