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

Posted zsychanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 删除数据库中反复数据(以部分数据为准)相关的知识,希望对你有一定的参考价值。

delete from zqzrdp 

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

运行,报错技术分享


异常意为:你不能指定目标表的更新在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 删除数据库中反复数据(以部分数据为准)的主要内容,如果未能解决你的问题,请参考以下文章

10分钟掌握 MySQL 优化技巧

linux下mysql忘记root密码解决方法

mysql删除重复数据,保留一条

oracle查询以当前年份为准的近些年数据

数据库部分(MySql)_1

怎样删除数据库中的反复记录?