网站建设资讯

NEWS

网站建设资讯

使用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,seq having count(*) > 1) and seq in (select 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)

上述内容就是使用SQL语句怎么删除重复记录并只保留一条,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网站标题:使用SQL语句怎么删除重复记录并只保留一条
文章网址:http://cdweb.net/article/jsgjdj.html