I have basic many to many relation set with tables categories, locations trough categories_locations table. Example:
Categories table
| ID | Name |
| 1 | Category 1 |
| 2 | Category 2 |
| 3 | Category 3 |
Locations table
| ID | Name |
| 1 | Location 1 |
| 2 | Location 2 |
| 3 | Location 3 |
Categories_Locations table
| category_id | location_id |
| 1 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 3 |
How to get all location that belong to category 2 and at the same time also belong to category 3? In above example that would result only to location 3!
Filtering with OR is simple. Just a normal left join where category_id IN (matched categories). But how to filter when I want to get only those relation that belong to category1 and at the same time also to category2 (and so on)?
1 Answer