求一条SQL语句,请大家帮忙

snowouldance 2011-06-28 08:38:59
有一张表,其中有字段A1,B1
A1不会有重复列,B1有重复列

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

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


...全文
64 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cd731107 2011-06-28
  • 打赏
  • 举报
回复
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)
--小F-- 2011-06-28
  • 打赏
  • 举报
回复
select * from tb t where exists (select 1 from tb where B1 = t.B1 and A1 <> t.A1)
AcHerat 2011-06-28
  • 打赏
  • 举报
回复

select *
from tb t
where 1 < (select count(*) from tb where B1 = t.B1)

-- try !
AcHerat 2011-06-28
  • 打赏
  • 举报
回复

select *
from tb
where B1 in (select distinct B1 from tb group by B1 having count(0) > 1)
AcHerat 2011-06-28
  • 打赏
  • 举报
回复

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

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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