First – apologies for the fuzzy title, I could not find a better one.
I have table with the following structure (simplification):
EmpID DeptID
1 1
1 2
2 1
3 2
4 5
5 2
This table represents a many-to-many relationship.
I’m interested in finding all the EmpIDs that are related to a specific group of DeptIDs, for example I want all the EmpIDs that are related to DeptIDs 1, 2 and 3. Please note it’s an AND relationship and not an OR relationship. For my case, the EmpID may be related to additional DeptIDs besides 1, 2 and 3 for it to be a valid answer.
The number of DeptIDs I’m interested in changes (i.e. I may want EmpIDs who’re related to both DeptID 3 and 5, or I may want EmpIDs related to DepIDs 2, 3, 4, 5, 6, 7).
When I try to approach this problem I find myself either creating a JOIN per DepID, or a subquery per DeptID. This would mean I have to generate a new query per the number of DeptIDs I’m testing against. I would obviously prefer having a static query with a parameter or set of parameters.
I’m working over both SQL Server and MySQL (developing in parallel two versions of my code).
Any ideas?
I’m assuming you want to find employees that are in ALL of the specified departments and not just the employees that are in ANY of the departments, which is a far easier query.
I’m going to preempt the inevitable suggestion that’ll come to use aggregation:
Resist that temptation. It’s significantly slower. A similar scenario to this came up in SQL Statement – “Join” Vs “Group By and Having” and the second version was, in that second, about twenty times slower.
I’d also suggest you look at Database Development Mistakes Made by AppDevelopers.