I have the following table, product_taxonomy_id
:
| product_id | taxonomy_id | value_id |
+------------+-------------+----------+
| 1 | 2 | 4 |
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 2 | 3 | 8 |
| 3 | 2 | 5 |
| 3 | 1 | 2 |
| 4 | 1 | 1 |
| 4 | 2 | 4 |
| 4 | 3 | 8 |
I want to get product_ids that have taxonomy = 1 and value_id = 1 AND taxonomy = 2 and value_id = 4.
The products that meet the first requirement are: 1, 2, 4.
From those, the requirements that meet the second requirement are: 1, 4
I am able to do it with the following query:
SELECT DISTINCT `product_id` FROM product_taxonomy_value
INNER JOIN (SELECT DISTINCT `product_id` FROM product_taxonomy_value WHERE `taxonomy_id` = 1 AND `value_id` = 1) a USING (`product_id`)
INNER JOIN (SELECT DISTINCT `product_id` FROM product_taxonomy_value WHERE `taxonomy_id` = 2 AND `value_id` = 4) b USING (`product_id`)
Is there a better solution?