MSSQL根据参数集选择不同的ID

时间:2017-02-22 18:22:24

标签: sql-server tsql

我确信这很简单,但这是一个艰难的一天: 我有一个如下数据表。我有一个属性ID列表,需要获得与所有选定的AttributeID匹配的不同ProductID。

ProductID   AttributeID
1           2
1           3
1           5
2           2
2           3
2           7

例如,我想要与属性2,3和5匹配的独特产品ID。

最佳方式是什么?

1 个答案:

答案 0 :(得分:1)

使用count(distinct ) = n其中nwhere AttributeId in ()列表中元素的数量。

select ProductId 
from t
where AttributeId in (2,3,5)
group by ProductId
having count(distinct AttributeId)=3
相关问题