David S. P. David S. P. - 2 months ago 7
MySQL Question

Get grouped ID with common values

It's possible to get all id_products with common id_feature_values?

id_feature | id_product | id_feature_value
---------- | ---------- | -----------------
17 | 4900 | 388
18 | 4900 | 398
20 | 4901 | 400
21 | 4901 | 401
17 | 4902 | 388
17 | 4902 | 398


For example, I want id_products with id_feature_value 388 and 398, so the result I need is 4900 and 4902.

Thank you!

Answer

This is the long way but it works:

SELECT DISTINCT id_product
FROM t
WHERE id_product IN
(SELECT id_product
 FROM t
 WHERE id_feature_value = 388)
AND id_product IN
(SELECT id_product
 FROM t
 WHERE id_feature_value = 398)

Tested here: http://sqlfiddle.com/#!9/cd642d/3