日期:2014-05-19  浏览次数:20436 次

请教个查询重复数据的问题
查询并显示出所有重复的数据
字段:
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