I'm stuck on the best way to achieve this MySQL query.
I have three tables:
products - this is a list of products
id | product_name
cats - this is a list of categories
id | category_name
cats_selected - multiple categories can be assigned to a product, this table lists them
cat_id | product_id
In a search a user can select multiple categories to search on at once. I want to check that a product exists in each category, so it needs to match cat_id and product_id. The cat id's are posted as an array.
What is the best way to query this data?
For example, the following doesn't work for obvious reasons:
SELECT *
FROM products
JOIN cats_selected
ON cats_selected.product_id = products.id
AND cats_selected.cat_id = '16372'
AND cats_selected.cat_id = '9'