首页 > 代码库 > sql查询重复记录、删除重复记录方法大全

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查询重复记录、删除重复记录方法大全