SQL: Many-To-Many table AND query

前端 未结 2 1809
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 19:57

First - apologies for the fuzzy title, I could not find a better one.

I have table with the following structure (simplification):

EmpID DeptID

1             


        
2条回答
  •  死守一世寂寞
    2020-12-05 20:39

    I'd start from something like:

    SELECT EmpID, COUNT(*) AS NumDepts
    FROM thetable
    WHERE DeptID IN (1, 2, 3)
    GROUP BY EmpId
    HAVING COUNT(*) == 3
    

    of course, that 3 in the last line would always be the length of the sequence of department ids you're checking (so for (2,3,4,5,6,7) it would be 6). This is one natural way to express "employees connected to all of these departments".

    Edit: I see a note in another answer about performance issues -- I've tried this approach in SQLite and PostgreSQL, with appropriate indices, and there it looks like it's performing well and with appropriate use of all said indices; and in MySQL 5.0, where I have to admit performance was nowhere as good.

    I suspect (without an opportunity to benchmark this on a zillion more engines;-) that other really good SQL engines (such as SQL Server 2008, Oracle, IBM DB2, the new open-source Ingres...) will also optimize this query well, while other mediocre ones (can't think of any with a popularity anywhere close to MySQL's) won't.

    So, no doubt your favorite answer will depend on what engines you really care about (this takes me back to the time, over a decade ago, when my responsibilities included managing the team which maintained a component that was supposed to provide well-performing queries over more than half a dozen disparate engines -- talk about nightmare jobs...!-).

提交回复
热议问题