gpt4 book ai didi

sql - 多个 LEFT JOIN - "left"表是什么?

转载 作者:行者123 更新时间:2023-11-29 11:19:42 25 4
gpt4 key购买 nike

我已经使用它了,所以是时候全面了解它了。假设这样的查询:

SELECT 
*
FROM a
LEFT JOIN b ON foo...
LEFT JOIN c ON bar...

documentation告诉我们

T1 { [INNER] | { LEFT | RIGHT | FULL } [OUTER] } JOIN T2 ON boolean_expression

LEFT OUTER JOIN

First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Thus, the joined table always has at least one row for each row in T1.

问题很简单:在这种情况下,T1 是什么?它是 a 吗?还是 a LEFT JOIN b ON foo? (或者,是一样的吗?)

最佳答案

FROM 子句从左到右解析条件(除非被括号覆盖)。所以:

FROM a 
LEFT JOIN b
ON foo...
LEFT JOIN c
ON bar...

解析为:

FROM (
a
LEFT JOIN b
ON foo...
)
LEFT JOIN c
ON bar...

这在 documentation 中有解释在 FROM 子句的 join-type 部分下:

Use parentheses if necessary to determine the order of nesting. In the absence of parentheses, JOINs nest left-to-right. In any case JOIN binds more tightly than the commas separating FROM-list items.

因此,一系列 LEFT JOIN 将所有记录保存在第一个提到的表中。这是一个方便。

请注意,无论连接类型如何,FROM 子句的解析都是相同的。

关于sql - 多个 LEFT JOIN - "left"表是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36135436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com