选择具有所有逗号分隔字符串的数据

时间:2019-03-14 19:09:00

标签: sql sql-server relational-division

我有一张类似的条件表

id   condition   member-id
 1    Fall         A1452
 2    Fall         A1453
 3    Dementia     A1452
 4    Dementia     A1453
 5    Fall         A1450
 6    Headaches    A1453

现在,我想要通过条件作为参数,例如“秋季,痴呆症” 我想要同时具有秋天痴呆条件

的那些会员ID。
select * from conditions where condition IN('Fall,Dementia')

这将返回3条记录,但是我需要同时拥有这两项的记录吗?

记住“秋天,痴呆症”是动态的,可能会更改为“秋天,痴呆症,头痛”

2 个答案:

答案 0 :(得分:2)

使用条件聚合

 select MemberId from 
 conditions
 where condition IN('Fall','Dementia')
 group by MemberId
 having count(distinct condition )=2 

答案 1 :(得分:0)

 select distinct MemberId 
 from StackOverflow4
 where Condition in ('Fall','Dementia')
 group by memberId
 having count(MemberId)=2