I’m trying to select products based on facets for a product category page.
The basic structure is:
Product_Facets -------------------------------- UID ProductID FacetID FacetOptionID 1 1 1 1 2 1 1 2 3 1 2 4 4 1 2 7
Products
--------------------------------
ProductID ProductName
1 Some Widget
I want to select all products which have a facet record set to the correct value for ALL of the user selected facets.
So if I have a request for:
Facet ID 1 set to value 6 AND
Facet ID 2 set to value 97 AND
Facet ID 5 set to value 43 AND
I want the query to get all products from the products table that have ALL of those facet records in the facets table for any given product. The query should not return products that only meet some of the requirements.
I think I need to do a sub-query inside of a having clause but I’m not sure how that gets structured?
One method would be using EXISTS clauses, which you could generate dynamically based on the request:
Another method would be straight inner joins (also easily generated dynamically):
Either method will only return records from Products where Product_Facets records exist for each requested FacetID and FacetOptionID (I assume this is the Value field you mentioned.)