如何去掉数据库重复记录并且只保留一条记录

 我来答
懂视生活
2023-07-27 · 百度认证:湖南福仁科技有限公司官方账号
懂视生活
向TA提问
展开全部

在n条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复并保留一条呢? 方法如下: 1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from peoplewhere peopleId in (select peopleId from people group by peopleId hav




在n条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复并保留一条呢?



方法如下:





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




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

2、删除表中多余的重复记录,重复记录是根据单个字段(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)

3、查找表中多余的重复记录(多个字段)



select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)



4、删除表中多余的重复记录(多个字段),只留有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)





5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录





select * 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)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式