日期:2014-05-18  浏览次数:20575 次

求一条SQL语句,请大家帮忙
有一张表,其中有字段A1,B1
A1不会有重复列,B1有重复列

例如
A1 B1
----------------
A AA1
B AA2
C AA2
D AA3

想写一条SQL语句,把所有B1字段重复的记录都选出来
结果
B AA2
C AA2




------解决方案--------------------
SQL code

select *
from tb t
where exists (select 1 from tb where B1 = t.B1 and A1 <> t.A1)

------解决方案--------------------
SQL code
select * from tb t where exists (select 1 from tb where B1 = t.B1 and A1 <> t.A1)

------解决方案--------------------
SQL code
select * from tb t where exists (select 1 from tb where B1 = t.B1 and A1 <> t.A1)

--或者
select * from tb where B1 in (select B1 from tb group by B1 having count(B1)>1)