I have a table structured as follows:
| product_id | category_id |
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
So:
- product 1 is in category 1 and 2
- product 2 is in category 1, 2 and 3
- product 3 is in category 1, 2, 3 and 4
- etc...
How can I structure an SQL Query that can retrieve a list of product_ids that are in:
- Category 4
- OR Category 2 but not Category 3 or 4
- OR Category 1 but not Category 2
I have tried:
SELECT *
FROM `catalog_category_product`
WHERE category_id = 1
OR (category_id = 2 AND category_id NOT IN (3,4))
OR (category_id = 1 AND category_id NOT IN (2,3))
GROUP BY product_id
... but this is not working as hoped.
Thanks very much in advance
Sarah