一个复杂的 mysql 连接


A complicated mysql join

好的,我有第一个表,其中包括:

表 1:id | depID(每个 ID 都有一个 depID)

然后,我有第二个表,其中我有表 2userID | depID(其中用户 ID 与单独行中的多个 depID 相关联。另外,我有表 3userID | rankID(其中用户 ID 与一个排名 ID 相关联)。

我需要从表 1 中获取所有 id 和 depID,然后检查表 2 的哪些用户 ID 共享相同的 depID(table1.depID = table2.depID),然后检查表 2 中的哪些用户 ID 具有 rankID = $rID

谢谢大家。

我认为这个SQL应该可以得到你想要的,但我不是100%清楚这个问题的措辞:

SELECT table2.userID
FROM table1
JOIN table2
ON table1.depID = table2.depID
JOIN table3
ON table2.userID = table3.userID
AND table3.rankID = $rID;