请问: 如何在数据库里显示重复的字段数据

ahuiok 2003-12-16 01:16:12
如: 一张表结构和数据如下:


id num
1 3
2 3
3 4
4 6
5 7
6 8
7 9
8 8
9 8

显示的结果是

id num
1 3
2 3
6 8
8 8
9 8
...全文
60 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmchw 2003-12-16
  • 打赏
  • 举报
回复
up
cloudchen 2003-12-16
  • 打赏
  • 举报
回复
select * from #temp where num in (select num from #temp group by num having count(num) > 1)
zjcxc 元老 2003-12-16
  • 打赏
  • 举报
回复
--测试
declare @t table(id int,num int)
insert into @t
select 1,3
union all select 2,3
union all select 3,4
union all select 4,6
union all select 5,7
union all select 6,8
union all select 7,9
union all select 8,8
union all select 9,8

--查询
select * from @t a
where exists(select 1 from @t where num=a.num and id<>a.id)

/*--结果
id num
----------- -----------
1 3
2 3
6 8
8 8
9 8

(所影响的行数为 5 行)

--*/
zjcxc 元老 2003-12-16
  • 打赏
  • 举报
回复
select * from table1 a
where exists(select 1 from table1 where num=a.num and id<>a.id)
goodluck001 2003-12-16
  • 打赏
  • 举报
回复
select * from table1
where num in
(select num from table1 group by num having sum(1)>1)
victorycyz 2003-12-16
  • 打赏
  • 举报
回复
不明白。
dlpseeyou 2003-12-16
  • 打赏
  • 举报
回复
select id=max(id),num from table1 group by num having sum(1)>1
wzh1215 2003-12-16
  • 打赏
  • 举报
回复
select * from table1 where num in(select num from table group by num having count(num)>1)
1ssp 2003-12-16
  • 打赏
  • 举报
回复
select * from table1 where num in(select num from table group by num having count(num)>1)
ivy_ou 2003-12-16
  • 打赏
  • 举报
回复
delete
from tablename temp1
where id <> (select max(id) from tablename temp2 where temp1.num = temp2.num)

删除表中的重复数据,保留重复数据id最大的记录
ahuiok 2003-12-16
  • 打赏
  • 举报
回复
能不能自动删除一行num相同的数据??

ahuiok 2003-12-16
  • 打赏
  • 举报
回复
看不太懂, 能再解释一下么?
cysh 2003-12-16
  • 打赏
  • 举报
回复
select a.id,x.num
from table1 as a left outer join (select num
from table1
group by num
having count(*)<>1) as x
on a.num=x.num
where x.num is not null

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧