您的位置:首页 > 数据库

sql 查出一张表中重复的所有记录数据

2016-05-26 18:00 267 查看

1.一张表中有id和name两个字段,查询出name重复的所有数据 。

select * from cui a where a.username in
(select username from cui group by username
having count(*)>1);


2.查询出所有数据进行分组之后,和重复数据的重复次数的查询数据。

select count(username) as '重复次数' ,username
from cui group by username having count(*)>1
order by username desc;


3.删除表中多余的重复记录,重复记录是根据单个字段peopleID来判断 ,只留有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(people)>1));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: