将 WHERE IN 参数视为 AND 而不是 OR

Treat WHERE IN parameters as AND and not OR

我有这个 table structure/data :

我想 select row_id(s) 具有 (97,6,2) AND (99,1, 4) 分别在 (attribute_id,store_id,value) 上。

在这个例子中,我们想要得到 row_id 8664,因为它符合这个条件。

我所做的是使用如下 where in 语句:

SELECT DISTINCT row_id from catalog_product_entity_int 
where row_id in 
   (select row_id from catalog_product_entity_int 
    WHERE (attribute_id, store_id,value) 
    IN ( (99,1,4),(97,6,1) ))

这输出具有 (99,1,4) OR (97,6,1).

的行

我试过这个查询:

SELECT DISTINCT row_id from catalog_product_entity_int 
where row_id in 
    (select row_id from catalog_product_entity_int 
    WHERE (attribute_id, store_id,value) IN (99,1,4) 
    AND (attribute_id, store_id,value) IN(97,6,1) )

但是我有#1241 - Operand should contain 3 column(s)

我如何设法 select 同时验证两个条件的行?

像这样写出你的 where 条件

WHERE (attribute_id = 99 AND store_id = 1 AND value = 4)
OR (attribute_id = 99 AND store_id = 6 AND value = 1)

尝试:

SELECT DISTINCT row_id FROM catalog_product_entity_int WHERE row_id IN 
(SELECT row_id FROM catalog_product_entity_int 
WHERE (attribute_id = 99 AND store_id = 1 AND value = 4)
OR (attribute_id = 97 AND store_id = 6 AND value = 1))

很简单。

向量 (99, 1, 4)(97, 6, 1) 必须在括号中:((99, 1, 4))((97, 6, 1)).

尝试:

SELECT DISTINCT row_id 
FROM catalog_product_entity_int 
WHERE row_id IN (
    SELECT row_id 
    FROM catalog_product_entity_int 
    WHERE (attribute_id, store_id, value) IN ((99, 1, 4))
          OR (attribute_id, store_id, value) IN ((97, 6, 1))
)

甚至更好:

SELECT DISTINCT row_id 
FROM catalog_product_entity_int 
WHERE row_id IN (
    SELECT row_id 
    FROM catalog_product_entity_int 
    WHERE (attribute_id, store_id, value) IN ((99, 1, 4), (97, 6, 1))
)