ms sql 2008 - 一个表,多个过滤器

时间:2015-08-06 18:11:32

标签: sql sql-server-2008 tsql

我希望过滤同时使用值{97的Id 17和值为11的Id 6的产品。

这是SQL Fiddle

在此示例中,我需要返回fkProductId = 24011

所在的行

你能帮我吗?

CREATE TABLE AttributeBindings(
    fkProductId int,
    fkAttributeId int,
    fkAttributeValueId int
)

INSERT INTO AttributeBindings (fkProductId, fkAttributeId ,fkAttributeValueId) 
                               VALUES (24011, 17, 97)
INSERT INTO AttributeBindings (fkProductId, fkAttributeId ,fkAttributeValueId) 
                               VALUES (24012, 17, 97)
INSERT INTO AttributeBindings (fkProductId, fkAttributeId ,fkAttributeValueId) 
                               VALUES (24011, 6, 11)

3 个答案:

答案 0 :(得分:0)

SELECT fkproductid FROM AttributeBindings
WHERE fkAttributeId = 17 AND fkAttributeValueId = 97
intersect
SELECT fkproductid FROM AttributeBindings
WHERE fkAttributeId = 6 AND fkAttributeValueId = 11

您可以使用INTERSECT来获得结果。

答案 1 :(得分:0)

您可以在产品ID上自行加入表格:

Select p1.fkProductId From AttributeBindings p1
    inner join AttributeBindings p2 on p1.fkProductId = p2.fkProductId
where 
    (p1.fkAttributeId = 17 and p1.fkAttributeValueId = 97)
    and (p2.fkAttributeId = 6 and p2.fkAttributeValueId = 11)

答案 2 :(得分:0)

这样的事情应该有效:

1

SQL Fiddle Demo