这是表格结构。我想要获取值为2和601的所有id_product
(此处id_product
= 5
)。如果我使用OR
将填充所有记录,这是不必要的。
id_product attribute_id Value
5 2 2
6 2 2
8 2 601
6 2 601
5 3 601
8 3 32
6 3 41
任何帮助将不胜感激。我不想使用子查询:-p
答案 0 :(得分:1)
您可以按查询使用分组:
select
id_product
from
tablename
where
attribute_id=2 and values in (2,601)
group by
id_product
having
count(*)=2
这将选择在两个不同的行中具有(attribute_id = 2和value = 2)或(attribute_id = 2和value = 601)的所有产品,然后它将计算返回的行数并仅选择具有的行两行(一行值为2,一行值为601)。
另一个选项(你的问题不太清楚)是使用这个where子句而不是上面的查询中的那个:
where
(attribute_id=2 and value=2) or
(attribute_id=3 and value=601)
答案 1 :(得分:0)
您可以在案例中使用此查询:
SELECT * FROM nameTable WHERE Values IN (2,601) and attribute_id = 2