sql查询重复记录删除重复记录方法大全
Posted 疯狂的小馒头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql查询重复记录删除重复记录方法大全相关的知识,希望对你有一定的参考价值。
1.查询重复记录单字段
select * from tbl a where exists(select 1 from tbl b where a.username=b.username group by username having count(*) > 1)
2.查询重复次数
select clistname,count(clistname) from clalist group by clistname having count(*)>1
3.删除重复记录,留有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)
4.查询重复记录,多字段
select *
from clalist a where exists
(
select 1 from clalist b
where a.ClalistID=b.ClalistID and a.SchSNo=b.SchSNo
group by ClalistID,SchSNo having count(*) > 1
)
order by SchSNo,ClalistID
5.删除重复记录,多字段
delete from clalist where exists
(select 1 from clalist b
where clalist.ClalistID=b.ClalistID and clalist.SchSNo=b.SchSNo
group by ClalistID,SchSNo having count(*) > 1)
and ClaListSNo not in (select min(ClaListSNo) from clalist group by ClalistID,SchSNo having count(*) > 1)
以上是关于sql查询重复记录删除重复记录方法大全的主要内容,如果未能解决你的问题,请参考以下文章