当前位置: 代码迷 >> Sql Server >> 求一条SQL语句,请大家帮忙,该怎么解决
  详细解决方案

求一条SQL语句,请大家帮忙,该怎么解决

热度:38   发布时间:2016-04-27 18:00:37.0
求一条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 twhere 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)
  相关解决方案