简体   繁体   English

我正在寻找在MySQL中组合多个联接操作以从2个以上的表中选择数据的背后逻辑

[英]I'm looking for the logic behind combining multiple join operations in MySQL to select data from more than 2 tables

I'm trying to wrap my head around all the different join operations in MySQL and found some very useful info already on this website when explained with venn diagrams. 我试图围绕MySQL中的所有不同联接操作进行研究,并在用维恩图进行解释时在此网站上已经找到了一些非常有用的信息。 I'm confused, however, on how to combine the SELECT JOIN statements when I want to select data from 3 or more tables. 但是,当我要从3个或更多表中选择数据时,我对如何组合SELECT JOIN语句感到困惑。

Does anyone have a good beginners reference for the logic behind this? 有人对此背后的逻辑有很好的初学者参考吗? Or is someone up to giving me a crash course? 还是有人愿意给我速成班?

Thank you 谢谢

You can join as many tables as you want if they have a relationship ! 如果有关系,您可以根据需要加入任意多个表!

Assume you have 3 Tables as example_A example_B and example_C the join query will look somewhat like this: 假设您有3个表作为example_A example_Bexample_B ,则example_C查询将如下所示:

Select a.JustAnycolumn,b.JustAnycolumn, c.* from example_A a
inner join example_B b on a.PrimaryKey=b.ForeignKey
inner join example_C c on c.PrimaryKey=a.b.ForeignKey

Note: The join condition depends on the relation between the tables 注意:连接条件取决于表之间的关系

In a way, joining three tables is no more hard to grasp than joining two tables. 在某种程度上,联接三个表并不比联接两个表更难掌握。

The JOIN operator is a binary operator in the same way that addition or multiplication are binary operators. JOIN运算符是二进制运算符,与加法或乘法是二进制运算符相同。 You can "chain" them. 您可以“链接”它们。

So just as you can use addition to sum up three or more terms: 因此,就像您可以使用加法来总结三个或更多术语一样:

2 + 4 + 3

You can use JOIN to join three or more tables: 您可以使用JOIN联接三个或更多表:

SELECT ...
FROM Books
JOIN Authors ON Books.author_id = Authors.author_id
JOIN Publishers ON Books.publisher_id = Publishers.publisher_id

You can visualize what this means: 您可以看到这意味着什么:

+---------+        +-------+        +------------+
| Authors |        | Books |        | Publishers |
+---------+        +-------+        +------------+
|    a    |<-------|   B   |---     |            |
|         |        |       |   |    |            |
|         |        |       |    --->|      p     |
+---------+        +-------+        +------------+

The purpose is to match up a row from Books to its related rows in Authors and Publishers, respectively. 目的是将“书籍”中的一行分别与“作者”和“出版者”中的相关行进行匹配。


Re your questions: 关于您的问题:

It seems weird to me that the from statement only occurs once 对我来说,奇怪的是from语句只出现一次

Does it also seem weird when you see an arithmetic expression like the following: 当您看到如下所示的算术表达式时,看起来是否也很奇怪:

sqrt( 2 + 4 + 1 )

Why does the sqrt only appear once? 为什么sqrt只出现一次? Because it's a function being applied to the result of an internal expression. 因为这是一个应用于内部表达式结果的函数。

Think of the FROM clause this way: 以这种方式考虑FROM子句:

"I am selecting columns from the following set of joined tables. First join them, then I'll select the columns I want." “我正在以下一组联接表中选择列。首先将它们联接,然后选择所需的列。”

Does the order of the join statements affect the outcome? join语句的顺序会影响结果吗?

No, the JOIN operation is commutative . 不,JOIN操作是可交换的 That is, just like 2+4 = 4+2 in addition, joins give the same result in either direction. 也就是说,就像另外2+4 = 4+2一样,联接在任一方向上给出的结果相同。 In fact, internally some SQL databases may choose to access the table in either order according to what makes it more efficient. 实际上,内部某些SQL数据库可能会根据提高效率的方式选择以任一顺序访问表。

The exception is an LEFT OUTER JOIN or RIGHT OUTER JOIN , which is not commutative. 例外是LEFT OUTER JOINRIGHT OUTER JOIN ,这是不可交换的。 The order matters. 顺序很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM