首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问个查询重复数据的有关问题

2012-01-19 
请教个查询重复数据的问题查询并显示出所有重复的数据字段:idAB1ab2ac3ab查询结果idAB1ab3ab[解决办法]sel

请教个查询重复数据的问题
查询并显示出所有重复的数据
字段:
id         A         B
1           a         b
2           a         c
3           a         b


查询结果
id         A         B
1           a         b
3           a         b

[解决办法]
select * from [Table] a where exists(select count(1) from [Table] where a=a.a and b=a.b group by a,b)> 1
[解决办法]
create table #t(id int,A char(1),B char(1))
insert into #t
select 1, 'a ', 'b '
union all select 2, 'a ', 'c '
union all select 3, 'a ', 'b '

select * from #t a
where exists(select 1 from #t b where a.A=b.A and a.B=b.B and a.id <> b.id)

drop table #t

/*
id A B
----------- ---- ----
1 a b
3 a b

(所影响的行数为 2 行)
*/
[解决办法]
select * from 表名 t where (select count(1) from 表名 where t.a=a and t.b=b) > 1
[解决办法]
---- 用ls的数据 ----
create table #t(id int,A char(1),B char(1))
insert into #t
select 1, 'a ', 'b '
union all select 2, 'a ', 'c '
union all select 3, 'a ', 'b '

select * from #t t where (select count(1) from #t where t.a=a and t.b=b) > 1

热点排行
Bad Request.