DODO
DODO

Reputation: 104

Query to get rows by multiple values

I have table for example like this:

id_product id_feature_value
1 60
2 60
1 40
2 10

I want get distinct id_products where id_feature_value = 60 and id_feature_value = 40 Its just example because I will need filter by more feature_values.

Upvotes: 0

Views: 108

Answers (2)

GMB
GMB

Reputation: 222582

If you want products that have both features, you can use group by and having:

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

This assumes no duplicates. Otherwise, you need count(distinct id_feature_value) = 2.

You can easily extend that to handle more features per product: you just need to expand the in clause, and modify the literal number in the having clause accordingly.

Upvotes: 2

eshirvana
eshirvana

Reputation: 24593

either using distinct and in :

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

or group by

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

Upvotes: 0

Related Questions