sql查询去掉重复记录
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查询,如何去除重复的记录?
首先,先说明一个问题。这样的结果出现,说明系统设计是有问题的。
其次
删除重复数据,你要提供你是什么数据库。
不同数据库会有不同的解决方案。
关键字Distinct
去除重复,如下列SQL,去除Test相同的记录;
1.
select
distinct
Test
from
Table
2.
如果是要删除表中存在的重复记录,那就逻辑处理,如下:
3.
select
Test
from
Table
group
by
Test
having
count(test)>1
4.
先查询存在重复的数据,后面根据条件删除
还有一个更简单的方法可以尝试一下:
select
aid,
count(distinct
uid)
from
表名
group
by
aid
这是sqlserver
的写法。
如图一在数据表中有两个膀胱冲洗重复的记录。
2
可以通过sql语句“select
*from
表名
where
编码
in(select
编码
from
表名
group
by
编码
having
count(1)
>=
2)”来查询出变种所有重复的记录如图二
3
通过sql语句"
delete
from
表名
where
编码
in(select
编码
from
表名
group
by
编码
having
count(1)
>=
2)
and
编码
not
in
(select
max(编码)from
表名
group
by
编码
having
count(1)
>=2)
"来删除重复的记录只保留编码最大的记录