mysql左连接,但需要第三个表中的值

mysql左连接,但需要第三个表中的值,mysql,left-join,Mysql,Left Join,我读过一百篇不同的“如何加入三张桌子”的帖子,但似乎无法让我的做到我想要的。这似乎不同于我在网上找到的示例,它们只是两个左连接 tests ----- id name platformA <<-- boolean, platformA supports test? platformB platformX testgroups <<-- tuple (testid,platid) is unique ---------- testid <<-- m

我读过一百篇不同的“如何加入三张桌子”的帖子,但似乎无法让我的做到我想要的。这似乎不同于我在网上找到的示例,它们只是两个左连接

tests
-----
id
name
platformA <<-- boolean, platformA supports test?
platformB
platformX

testgroups  <<-- tuple (testid,platid) is unique
----------
testid      <<-- matches 'id' in 'tests' table above
platid      <<-- matches id in 'platforms' table below
grouptype

platforms
---------
id
name <<-- corresponds to column name 'platform?' in 'tests'
这不起作用,因为testgroup可以有多个testid=2,每个platid=1??。元组被保证是唯一的,但不是任何一列本身。因此,对于每个tests.name,它为每个平台提供了一行。我需要通过平台名(比如platformA)来限制它,我知道平台名,但在表testgroups中我无权访问平台名。因此,如果我知道platid而不是平台名称,我可以这样做,这是可行的:

select tests.name, testgroups.grouptype from tests
left join testgroups on (tests.id = testgroups.testid and testgroups.platid = 27)
where tests.platformA = 1;
我知道我只想要platformA,但如果不在platformA表中查找它,我不知道它的id是(在上面的示例中)27。如何将其纳入我的查询中?我试过很多组合,但没有一个是完全正确的。下面是一个我认为可行的例子:

select tests.name, testgroups.grouptype from tests, platforms
left join testgroups on (tests.id = testgroups.testid and platforms.name = 'platformA')
where tests.platformA = 1;
这似乎是非法的语法。我还尝试了多个左连接,这对我不起作用

我很喜欢这个答案,但也很想解释一下为什么它会起作用,因为我已经撞了我的头一段时间了

谢谢, 大卫

==更新====

我认为@Marc几乎是对的,但它限制了我的输出,因为testgroups.platid中有该平台的数据

以下是我尝试使用他的答案并给出完整的问题:

select tests.name, testgroups.grouptype from tests
left join testgroups on (tests.id = testgroups.testid)
left join platforms on (testgroups.platid = platforms.id)
where tests.platformA = 1 and (platforms.name = 'platformA' or platforms.id is null);

在一个查询中有多个联接,与上一个示例中完全相同。。。除非您不应该混合连接样式

select tests.name, testgroups.grouptype
from tests
left join testgroups on tests.id = testgroups.testid
left join platforms ON ......
where tests.platformA = 1 and platforms.name = 'platformA';

好吧,我会给Marc B一个正确的答案,尽管你应该阅读我的更新,因为它是更正确的答案。
select tests.name, testgroups.grouptype
from tests
left join testgroups on tests.id = testgroups.testid
left join platforms ON ......
where tests.platformA = 1 and platforms.name = 'platformA';