在开始之前,是的,我彻底尝试过搜索许多关于 JOINS/INNER JOINS/OUTER JOINS/FULL JOINS 的教程,但我不太确定我在寻找什么。指导或简单地指出我正确的方向将会非常有帮助。我会尽量说得清楚。
基本上,我有三张 table
福
| FooID | name | data |
1 Name1 Data1
2 Name2 Data2
3 Name3 Data3
4 Name4 Data4
酒吧
| BarID |
1
2
矩阵
| BarID | FooID|
1 2
1 3
1 4
2 1
2 3
所以我要寻找的是,我基本上有 BarID (为了清楚起见,让我们假装它是 1)。我想从表 Matrix 中获取与 BarID 相关的所有行,这样我就可以检索与 Foo 中相关的行(例如, BarID = 1,所以我应该在 Foo 中获取第 2、3 和 4 行,如果 BarID 为 2,我将获取 1 和 3,依此类推)。
我正在尝试类似的事情:
SELECT Foo.FooID, Foo.name, Foo.data
FROM Bar
JOIN Matrix ON Matrix.BarID = 1 // The 1 is passed in, in this example
JOIN Foo ... // And this is where I'm stuck
这对我想要实现的目标有意义吗?我知道这很奇怪。将不胜感激任何帮助指点。预先感谢您!
最佳答案
您似乎想要一个简单的 JOIN
和 WHERE
select f.*
from foo f join
matrix m
on f.fooid = m.fooid
where m.barid = 1;
您不需要 bar
表,因为您正在传递 id。我认为你可能想得太多了。
关于mysql - SQL - 通过第三个表中的公共(public)链接连接三个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47185907/