I have two tables:
products
id name
1 Product 1
2 Product 2
3 Product 3
products_sizes
id size_id product_id
1 1 1
2 2 1
3 1 2
4 3 2
5 3 3
So product 1 has two sizes: 1, 2. Product 2 has two sizes: 1, 3. Product 3 has one size: 3.
What I want to do is build a query that pulls back the products that have both size 1 and size 3 (i.e. Product 2). I can easily create a query that pulls back the products that have both sizes 1 AND 3:
select `products`.id, `products_sizes`.`size_id`
from `products` inner join `products_sizes` on `products`.`id` = `products_sizes`.`product_id`
where products_sizes.size_id IN (1, 3)
group by products.id
When I run this query, I get back Product 1, Product 2, and Product 3.
Just to reiterate, I'd like to only get back Product 2. I've tried using the HAVING clause, messing around with $id IN GROUP_CONCAT(...) but I haven't been able to get anything to work. Thanks in advance, guys.