一个既包含a又包含b 的sql 语句

/*
一个查询包含的sql 语句
条件:任意一个id,例:id=1
需求:得到name值包含条件id的name的id 例:既包含a又包含b的id
思路:一开始想着用 join 或者 exists 来解决,费劲不少,解决了
	  但是问题是id不固定,name多少不固定..........
	  如果是id=n又有a,b又有c,d,e... 要写5个6个n个join还是exists.......
*/
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[name] varchar(1))
insert [tb]
select 1,'a' union all
select 1,'b' union all
select 2,'a' union all
select 2,'c' union all
select 3,'a' union all
select 3,'b' union all
select 3,'c' union all
select 4,'a' union all
select 4,'b' union all
select 4,'d' union all
select 5,'a' union all
select 5,'b' union all
select 5,'c' union all
select 5,'d' 
go

select [id] from [tb] where [name] in (select [name] from [tb] where [id]=1)
group by [id]
having count(distinct [name])>=(select count(1) from [tb] where [id]=1)


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值