有问题的代码:
...and crset.Name IN (Select [part] from [dbo].[SplitString](@Type, ',')...
假设@Type
为'Blue,White'
,并且splitstring函数允许我将该字符串转换为使用IN的列表。
目前,这将返回所有具有蓝色或白色的条目,比如说房屋颜色。我想要的是一种方式来说“只有那些颜色在列表中并且必须具有列表中所有颜色的房屋。这意味着每个房屋有多个条目:每个房间有一个条目(这就是数据库中的情况。这意味着有“红色”和“白色”条目的房子不会出现,只有“蓝色”的条目不会显示,“白色”的条目不会显示但是任何有多个记录(蓝色,白色)的房子都会。
如果您需要更多代码,请与我们联系。
答案 0 :(得分:3)
我会通过使用CTE来做到这一点:
with vals as (
Select part from [dbo].[SplitString](@Type, ',')
)
select t.house
from table t
where t.color in (select part from vals)
group by t.house
having count(distinct t.color) = (select distinct part from vals);
如果要将匹配限制为完全颜色,请删除where
子句。
答案 1 :(得分:2)
select house
from crset
where crset.Name IN (Select [part] from [dbo].[SplitString](@Type, ','))
group by house
having count(distinct crset.Name) = (Select count([part]) from [dbo].[SplitString](@Type, ','))
您可以按house
进行分组,并计算是否有与您提供的列表一样多的颜色。如果是这样,那么您在where
条件中过滤的所有颜色也在列表中。