博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用SQL语句,删除掉重复项只保留一条
阅读量:6554 次
发布时间:2019-06-24

本文共 1493 字,大约阅读时间需要 4 分钟。

hot3.png

用SQL语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用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 peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>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)

6.消除一个字段的左边的第一位:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'

7.消除一个字段的右边的第一位:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录

update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId

转载于:https://my.oschina.net/artong999/blog/552460

你可能感兴趣的文章
C语言 HTTP上传文件-利用libcurl库上传文件
查看>>
[MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API
查看>>
Hibernate 的HQL语句,初级
查看>>
调试逆向分为动态分析技术和静态分析技术(转)
查看>>
Android webview使用详解
查看>>
业务对象和BAPI
查看>>
程序源系统与当前系统不一致:Carry out repairs in non-original systems only if urgent
查看>>
微软职位内部推荐-Senior Software Engineer
查看>>
程序中的魔鬼数字
查看>>
SVN高速新手教程
查看>>
session cookie
查看>>
如何在Vblock里配置Boot from SAN
查看>>
ZBar之ZBarReaderViewController
查看>>
Android学习笔记——Handler(一)
查看>>
Nuget~管理自己的包包~丢了的包包快速恢复
查看>>
Hadoop单机模式安装-(3)安装和配置Hadoop
查看>>
$.extend({},defaults, options) --(初体验三)
查看>>
自己主动瀑布流布局和实现代码加载
查看>>
maven的一些依赖
查看>>
腾讯云短信服务使用记录与.NET Core C#代码分享
查看>>