本篇文章重点为大家讲解一下sql查询重复记录、删除重复记录具体方法,有需要的小伙伴可以参考一下。
查找所有重复标题的记录:
代码语言:javascript复制SELECT *
FROM t_info a
WHERE ((SELECT COUNT(*)
FROM t_info
WHERE Title = a.Title) > 1)
ORDER BY Title DESC
一、查找重复记录
1.查找全部重复记录
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
2.过滤重复记录(只显示一条)
Select * From HZT Where ID In (Select Max(ID) From HZT Group By Title)
注:此处显示ID最大一条记录
二、删除重复记录
1.删除全部重复记录(慎用)
Delete 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
2.保留一条记录
Delete HZT Where ID Not In (Select Max(ID) From HZT Group By Title)
注:此处保留ID最大一条记录
删除多余的重复记录
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
代码语言:javascript复制select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
代码语言:javascript复制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.查找表中多余的重复记录(多个字段)
代码语言:javascript复制select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4.删除表中多余的重复记录(多个字段),只留有rowid最小的记录
代码语言:javascript复制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最小的记录
代码语言:javascript复制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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:zbxhhzj@qq.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。 转载本站文章请保留原文链接,如文章内说明不允许转载该文章,请不要转载该文章,谢谢合作。