查询基于具有多个值的第二列查找具有多个条目的ID?

时间:2014-04-14 14:21:25

标签: mysql

我有以下问题我想从feature_values表中选择product_id,其中variant_id等于多个值

我有这个

SELECT product_id FROM feature_values WHERE variant_id = '162' AND variant_id = '11819'

以上不会返回结果

以下两个查询返回product_id

SELECT product_id FROM feature_values WHERE variant_id = '162'

和这个

SELECT product_id FROM feature_values WHERE variant_id = '11819'

表示例数据

Feature ID, Product ID, Variant ID, Value
        92,        565,      11815
        69,        565,        162
        92,        566,      11819
        69,        566,        162

2 个答案:

答案 0 :(得分:1)

SELECT product_id FROM feature_values WHERE variant_id = '162' OR variant_id = '11819'

SELECT product_id FROM feature_values WHERE variant_id in ('162','11819') 

答案 1 :(得分:1)

尝试这些,并不是你想要的,想想逻辑变体id可以是一个或另一个,而不是两个同时。

SELECT product_id FROM feature_values WHERE variant_id IN ('162', '11819')

OR

SELECT product_id FROM feature_values WHERE variant_id = '162' OR variant_id = '11819'