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)
"来删除重复的记录只保留编码最大的记录
其次
删除重复数据,你要提供你是什么数据库。
不同数据库会有不同的解决方案。
关键字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)
"来删除重复的记录只保留编码最大的记录
展开全部
sql查询去除重复值语句
sql
单表/多表查询去除重复记录
单表distinct
多表group
by
group
by
必须放在
order
by
和
limit之前,不然会报错
************************************************************************************
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select
*
from
people
where
peopleId
in
(select
peopleId
from
people
group
by
peopleId
having
count(peopleId)
>
1)
2、删除表中多余的重复记录,重复记录是根据单个字段(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(peopleId
)>1)
3、查找表中多余的重复记录(多个字段)
select
*
from
vitae
a
where
(a.peopleId,a.seq)
in
(select
peopleId,seq
from
vitae
group
by
peopleId,seq
having
count(*)
>
1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete
from
vitae
a
where
(a.peopleId,a.seq)
in
(select
peopleId,seq
from
vitae
group
by
peopleId,seq
having
count(*)
>
1)
and
rowid
not
in
(select
min(rowid)
from
vitae
group
by
peopleId,seq
having
count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select
*
from
vitae
a
where
(a.peopleId,a.seq)
in
(select
peopleId,seq
from
vitae
group
by
peopleId,seq
having
count(*)
>
1)
and
rowid
not
in
(select
min(rowid)
from
vitae
group
by
peopleId,seq
having
count(*)>
sql
单表/多表查询去除重复记录
单表distinct
多表group
by
group
by
必须放在
order
by
和
limit之前,不然会报错
************************************************************************************
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select
*
from
people
where
peopleId
in
(select
peopleId
from
people
group
by
peopleId
having
count(peopleId)
>
1)
2、删除表中多余的重复记录,重复记录是根据单个字段(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(peopleId
)>1)
3、查找表中多余的重复记录(多个字段)
select
*
from
vitae
a
where
(a.peopleId,a.seq)
in
(select
peopleId,seq
from
vitae
group
by
peopleId,seq
having
count(*)
>
1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete
from
vitae
a
where
(a.peopleId,a.seq)
in
(select
peopleId,seq
from
vitae
group
by
peopleId,seq
having
count(*)
>
1)
and
rowid
not
in
(select
min(rowid)
from
vitae
group
by
peopleId,seq
having
count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select
*
from
vitae
a
where
(a.peopleId,a.seq)
in
(select
peopleId,seq
from
vitae
group
by
peopleId,seq
having
count(*)
>
1)
and
rowid
not
in
(select
min(rowid)
from
vitae
group
by
peopleId,seq
having
count(*)>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关键字Distinct 去除重复,如下列SQL,去除Test相同的记录;
1.
select distinct Test from Table
2.
如果是要删除表中存在的重复记录,那就逻辑处理,如下:
3.
select Test from Table group by Test having count(test)>1
4.
先查询存在重复的数据,后面根据条件删除
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
的写法。。。
select
aid,
count(distinct
uid)
from
表名
group
by
aid
这是sqlserver
的写法。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |