I have three mysql tables
items
===========
id title
items_in_categories
============================
id item_id category_id
categories
===========
id title
I want to find all the items that belong to ALL the stated categories. Not any one category, but ALL categories
Eg, if I want to search all the items that belongs to category id 3 and 5
the no. of possible categories to be searched can go up to as many as 20.
Eg, I want to get all the items that belongs to category id 1, 2, 3, 4, 5, 6, …. and 20
I would like to use as simple a way as possible.
I have tried AND and a nested NOT EXISTS as stated in the mysql manual.
Nothing worked.
UPDATE:
I came up with this.
select * from
(select count(*) as counter, item_id from items_in_categories
where category_id in (3, 5) group by item_id)getall
where counter = 2
Is there a better way?
I know this can be prettied up and put into one query (You could nest the category count within the having instead of as a separate query…I just think this is more readable), but here is my shot at this: