I've got 3 tables:
activities, tools and as these share a n:m relation a table activities_tools.
Users on my website can select available tools on a filter. I'm now trying to get all activities, where the according tools are available.
So e.g. if I'm selecting tools with the id's 1 and 2 as available, I should be getting activities requiring either no tools, tool 1, tool 2 as well as activities that require both. Activities requiring other tools should be therefore excluded.
My query I have tried with so far looks like this:
SELECT activities.*, GROUP_CONCAT(tool) AS tools
FROM activities
LEFT JOIN activities_tools ON activities_tools.activity = activities.id
WHERE ISNULL(tool) OR tool IN (1,2)
GROUP BY activities.id
The (1,2) is the list of id's selected by the user.
The ISNULL part solves the problem of getting activities which have no tools applied. My issue now it though, that e.g. a tool having tools 1 AND 2 as required is getting returned when only one of them is available.
How can I check it against multiple values at once? I have tried fiddling with GROUP_CONCAT + HAVING + FIND_IN_SET and more.. but I can't come up with a solution.
Any help is appreciated.