-The question itself might not describe the issue well, but I'm not sure how to present it
I have implemented this design ,so I have the following table diagram.
+---------------+ +-------------------+
| PRODUCTS |-----< PRODUCT_VARIANTS |
+---------------+ +-------------------+
| #product_id | | #product_id |
| product_name | | #variant_id |
+---------------+ | price |
| +-------------------+
| |
+--------^--------+ +--------^--------+
| PRODUCT_OPTIONS |-----< VARIANT_VALUES |
+-----------------+ +-----------------+
| #product_id | | #product_id |
| #option_id | | #variant_id |
+--------v--------+ | #option_id |
| | option_value_id|
+-----------------+ +--------v--------+
| OPTIONS | |
+-----------------+ |
| #option_id | |
| option_name | |
+-----------------+ |
| |
+-------^---------+ |
| OPTION_VALUES |-------------+
+-----------------+
| #option_id |
| #option_value_id|
| value_name |
+-----------------+
fields with "#" are the Primary Keys
I want to afford users to filter products using option_values
. say I have the following product variant shirtA(product) size(option) small(option_value), color(option) green(option_value)
, and a user would like to filter for all products that have are say small and red
shirts, so the previous product variant should not be included.
I have tried doing this
SELECT p.product_id, p.title, p.description FROM product_variants pv
JOIN products p ON pv.product_id = p .product_id
JOIN variant_values vv ON vv.product_variant_id = pv.product_variant_id
WHERE vv.option_value_id IN (6, 10)
GROUP BY (p.product_id, p.title, p.description);
but as you can see it includes the products that hold option_value_id
6 or 10 and not both, so how to remove products that don't have both option_values not just one?
UPDATE 1
to clarify what I need to do I want is : enforce all products selected to have all the option value ids the user sends (in a form of an array) WHERE vv.option_value_id IN (6, 10)
. the idea is just like in most of e-commerce websites when user filters for his/her preferences option_values
, so user might want to search for all products that are large (let's say that is option_value_id "6") and red (option_value_id "10") and maybe green...
and so on. so I don't want any product that only has a "red" color but doesn't have "large" size to be returned, and if user added another say color to the filter say "green" color then all the returned products should match all three filters (having green and red colors and large size).