码迷,mamicode.com
首页 > 数据库 > 详细

MySQL 删除数据库中重复数据(以部分数据为准)

时间:2014-07-08 19:03:26      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:数据库

delete from zqzrdp 

where tel  in (select min(dpxx_id) from  zqzrdp  group by tel  having count(tel)>1);

执行,报错bubuko.com,布布扣


异常意为:你不能指定目标表的更新在FROM子句。傻了,MySQL 这样写,不行,让人郁闷。

难倒只能分步操作,蛋疼

以下是网友写的,同样是坑爹的代码,我机器上运行不了。

1. 查询需要删除的记录,会保留一条记录。

 代码如下 复制代码

select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as  bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where  a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid

2. 删除重复记录,只保留一条记录。注意,subject,RECEIVER 要索引,否则会很慢的。

 代码如下 复制代码

delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as  bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid;


3. 查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

 代码如下 复制代码

select * from people
where peopleId in (select  peopleId  from  people  group  by  peopleId  having  count(peopleId) > 1)


4. 删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

 代码如下 复制代码

delete from people 
where peopleId  in (select  peopleId  from people  group  by  peopleId   having  count(peopleId) > 1)
and rowid not in (select min(rowid) from  people  group by peopleId  having count(peopleId )>1)

5.删除表中多余的重复记录(多个字段),只留有rowid最小的记录

 代码如下 复制代码
delete from vitae a
where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

MySQL 删除数据库中重复数据(以部分数据为准),布布扣,bubuko.com

MySQL 删除数据库中重复数据(以部分数据为准)

标签:数据库

原文地址:http://blog.csdn.net/u011598529/article/details/37519585

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!