JOIN 语句 (SQL) 中的“左”表和“右”表究竟是哪个表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4109704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which table exactly is the "left" table and "right" table in a JOIN statement (SQL)?
提问by Jake
What makes a given table the left table?
什么使给定的表成为左表?
Is it that the table is indicated in the "From" part of the query?
是否在查询的“From”部分指示了该表?
Or, is it the left table because it is on the left hand side of the = operator?
或者,它是左表,因为它位于 = 运算符的左侧?
Are the following equivalent
以下是否等价
SELECT *
FROM left_table
LEFT JOIN right_table ON left_table.right_id = right_table.id
and
和
SELECT *
FROM left_table
LEFT JOIN right_table on right_table.left_id = left_table.id
???
???
Thanks
谢谢
采纳答案by Paul Sonier
The Left table is the first table in the select. Yes, your two examples are equivalent.
左表是选择中的第一个表。是的,你的两个例子是等价的。
回答by mbillard
The right table is always the table that you are joining on. So yes, both of your statements are equivalent.
正确的表始终是您加入的表。所以是的,你的两个陈述是等价的。
JOIN [Table] ON ...
[Table] is always the right table.
[表] 始终是正确的表。
回答by Paulo Scardine
Roughly "left" is the result of everything that appears first in the whole FROM clause when reading from left to right - including the result of other JOINs, sub-queries, VIEWs and STORED PROCEDURES.
大致“左”是从左到右阅读时整个 FROM 子句中最先出现的所有内容的结果 - 包括其他 JOIN、子查询、视图和存储过程的结果。
Both SQL statements are equivalent because the =
operator at the ON
part of the JOIN
clause is symmetric(if a = b then b = a) so the result is the same no matter the order.
两个 SQL 语句是等效的,因为子句部分的=
运算符是对称的(如果 a = b 则 b = a),因此无论顺序如何,结果都是相同的。ON
JOIN
The regular join shows only the lines where the ON clause of the JOIN is true, while the LEFT JOIN shows also the records from "left" if the condition is false (showing NULL for any column from "right" present in the SELECT).
常规连接仅显示 JOIN 的 ON 子句为真的行,而 LEFT JOIN 还显示来自“左”的记录,如果条件为假(对于 SELECT 中存在的“右”的任何列显示 NULL)。
For example:
例如:
-- People: -- Car
id | name owner_id | model
---+------------ ---------+------------
1 | Paul 1 | Ferrari
2 | Nancy 2 | Porsche
3 | Arthur NULL | Lamborghini
4 | Alfred 10 | Maserati
> select people.name, car.model from people join car on car.owner_id=people.id;
name | model
---------+--------------
Paul | Ferrari
Nancy | Porsche
2 record(s) found
> select people.name, car.model from people left join car on
car.owner_id=people.id;
name | model
---------+--------------
Paul | Ferrari
Nancy | Porsche
Arthur | NULL
Alfred | NULL
4 record(s) found
> select people.name, car.model from people left join car on
people.id = car.owner_id;
name | model
---------+--------------
Paul | Ferrari
Nancy | Porsche
Arthur | NULL
Alfred | NULL
4 record(s) found
回答by Gabriel Magana
See this for a pretty good walkthrough on joins: http://en.wikipedia.org/wiki/Join_(SQL)
请参阅有关连接的非常好的演练:http: //en.wikipedia.org/wiki/Join_(SQL)
And yes, both statements are equivalent :-)
是的,这两个陈述是等效的:-)
回答by Dan J
Yes, it's determined by the side of the JOIN operator the table appears on. Your two examples are indeed equivalent.
是的,它由表出现的 JOIN 运算符的一侧决定。你的两个例子确实是等价的。