2016-04-13 46 views
0

首先,请对我感到高兴,我是一个初学SQL语言和英语的人。 所以我有这个问题。基于参数的mysql高级查询选择

我创建了sqlfiddle。 我的查询看起来像它的正常工作,但我觉得它是不工作

我想编写一个查询,返回基于参数的产品ID的变种,将发送

正确的查询结果会是什么样的以下

PARAM = 6 

id product_id productvariant_id attributevalue_id 

1  3    1      6 
2  3    2      6 
---- BAD RESULTS ----- 
3  3    3      6 
4  3    3      9 
6  3    5      6 
7  3    6      6 
8  3    6     11 

PARAM = 6,9 

id product_id productvariant_id attributevalue_id 

3  3    3     6 
4  3    3     9 
---- BAD RESULTS ----- 
3  3    3     6 
4  3    3     9 
6  3    5     6 
7  3    6     6 
8  3    6     11 

我真正需要的是回归productvariant_id含有插入则params的组合,如果我只发送一个attributevalue_id,我需要找到productvariant其中只包含一个attributevalue_id。如果我发送两个参数,我发现两个..的组合..不多或少

回答

0

不幸的是,我没有足够的评论声誉,因此我必须写这个答案。 你能澄清你的目标吗?

是否要检索attributevalue_id与您的参数匹配的所有数据集的productvariant_id?

SELECT productvariant_id 
FROM productvariantattribute 
WHERE attributevalue_id IN ($YOUR_PARAMETER_LIST); 

还是你想找回在productvariant_id只有你指定为参数,这些attributevalue_ids所有数据集的productvariant_id?

SELECT `productvariant_id` 
FROM `productvariantattribute` 
WHERE `attributevalue_id` IN ($YOUR_PARAMETER_LIST) 
AND `productvariant_id` NOT IN (
    SELECT `productvariant_id` 
    FROM `productvariantattribute` 
    WHERE `attributevalue_id` NOT IN ($YOUR_PARAMETER_LIST) 
)