查询以按多个值获取行

Query to get rows by multiple values

我有 table 例如这样的:

id_product id_feature_value
1 60
2 60
1 40
2 10

我想要不同的 id_products 其中 id_feature_value = 60 和 id_feature_value = 40 这只是一个例子,因为我需要按更多 feature_values.

进行过滤

使用 distinct 和 in :

select distinct id_products
from tablename
where id_feature_value IN ( 40, 60 , ...)

或分组依据

select id_products
from tablename
where id_feature_value IN ( 40, 60 , ...)
group by id_products

如果您想要同时具有这两种功能的产品,您可以使用 group byhaving:

select id_product
from mytable
where id_feature_value in (40, 60)
group by id_product
having count(*) = 2

假设没有重复。否则,您需要 count(distinct id_feature_value) = 2.

您可以轻松扩展它以处理每个产品的更多功能:您只需扩展 in 子句,并相应地修改 having 子句中的文字数字。