我是靠谱客的博主 机智仙人掌,最近开发中收集的这篇文章主要介绍SQL如何删除重复数据,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

本教程操作环境:windows7系统、mysql8.0版本、Dell G3电脑。

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

在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢

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

select * from people 
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
登录后复制

扩展:

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

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

select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
登录后复制

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

查找表中多余的重复记录(多个字段),不包含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)
登录后复制

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

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'
登录后复制

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

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'
登录后复制

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

update vitae set ispass=-1where peopleId in (select peopleId from vitae group by peopleId
登录后复制

相关推荐:《mysql教程》

以上就是SQL如何删除重复数据的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是机智仙人掌为你收集整理的SQL如何删除重复数据的全部内容,希望文章能够帮你解决SQL如何删除重复数据所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部