简体   繁体   English

哪个表恰好是JOIN语句(SQL)中的“左”表和“右”表?

[英]Which table exactly is the “left” table and “right” table in a JOIN statement (SQL)?

What makes a given table the left table? 什么使得给定的表成为左表?

Is it that the table is indicated in the "From" part of the query? 该表是否在查询的“发件人”部分中指示?

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 谢谢

The Left table is the first table in the select. Left表是select中的第一个表。 Yes, your two examples are equivalent. 是的,你的两个例子是等价的。

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. [表]始终是正确的表格。

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,子查询,VIEW和STORED PROCEDURES的结果。

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语句都是等价的,因为JOIN子句的ON部分的=运算符是对称的 (如果a = b然后b = a),那么无论顺序如何,结果都是相同的。

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还显示“left”的记录(对于SELECT中存在的“right”,任何列显示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

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 :-) 是的,这两个陈述都是等价的:-)

Yes, it's determined by the side of the JOIN operator the table appears on. 是的,它由JOIN运算符一侧确定,表格出现在。 Your two examples are indeed equivalent. 你的两个例子确实是等价的。

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

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