首页 > 数据库技术 > 详细

sql查询重复记录、删除重复记录方法大全

时间:2017-02-16 22:22:12      阅读:191      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/wangdodo/p/6407002.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!