sql查询去掉重复记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql查询去掉重复记录相关的知识,希望对你有一定的参考价值。

参考技术A

1、打开要去掉重复数据的数据库,这里新建一张含有重复数据的user表做示例,如下图所示:

2、输入“select * from user where name in (select name from user group by name having count(name) > 1) ”sql语句,点击运行可以看到查询出了数据库中user表的重复数据。

3、通过“delete from user where   name in (select name from user group by name  having count(name) > 1) ”sql语句删除姓名重复的数据。

4、也可以通过“select distinct name from user”sql语句来去掉重复数据,这里去掉了张三的重复数据。

5、通过“select distinct class from user”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查询去掉重复记录的主要内容,如果未能解决你的问题,请参考以下文章

sql查询消除重复记录

sql查询两表中不重复记录

SQL查询,根据日期去掉重复ID

sql 如何过滤重复记录

sql查询中如何去除某个字段重复的数据

SQL查询,如何去除重复的记录?