简体   繁体   English

解释在JOIN语句中选择“FROM”的表

[英]Explain which table to choose “FROM” in a JOIN statement

I'm new to SQL and am having trouble understanding why there's a FROM keyword in a JOIN statement if I use dot notation to select the tables.columns that I want. 我是SQL的tables.columns ,如果我使用点符号选择我想要的tables.columns ,我很难理解为什么JOIN语句中有一个FROM关键字。 Does it matter which table I choose out of the two? 我从两个表中选择哪个表格是否重要? I didn't see any explanation for this in w3schools definition on which table is the FROM table. 我在w3schools定义中没有看到任何解释,哪个表是FROM表。 In the example below, how do I know which table to choose for the FROM ? 在下面的示例中,我如何知道为FROM选择哪个表? Since I essentially already selected which table.column to select, can it be either? 由于我基本上已经选择了哪个table.column来选择,它可以是吗?

For example: 例如:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

The order doesn't matter in an INNER JOIN . INNER JOIN的顺序无关紧要。

However, it does matter in LEFT JOIN and RIGHT JOIN . 但是,它在LEFT JOINRIGHT JOIN In a LEFT JOIN , the table in the FROM clause is the primary table; LEFT JOINFROM子句中的表是主表; the result will contain every row selected from this table, while rows named in the LEFT JOIN table can be missing (these columns will be NULL in the result). 结果将包含从此表中选择的每一行,而LEFT JOIN表中指定的行可能会丢失(结果中这些列将为NULL )。 RIGHT JOIN is similar but the reverse: rows can be missing in the table named in FROM . RIGHT JOIN类似但反过来:在FROM中命名的表中可能缺少行。

For instance, if you change your query to use LEFT JOIN , you'll see customers with no orders. 例如,如果您将查询更改为使用LEFT JOIN ,您将看到没有订单的客户。 But if you swapped the order of the tables and used a LEFT JOIN , you wouldn't see these customers. 但是如果您交换了表的顺序并使用了LEFT JOIN ,那么您将看不到这些客户。 You would see orders with no customer (although such rows probably shouldn't exist). 您会看到没有客户的订单(尽管这些行可能不存在)。

The from statement refers to the join not the table. from语句引用的是连接而不是表。 The join of table will create a set from which you will be selecting columns. 表的连接将创建一个集合,您将从中选择列。

For an inner join it does not matter which table is in the from clause and which is in the join clause. 对于inner joinfrom子句中的哪个表以及join子句中的哪个表无关紧要。 For outer join s it of course does matter, as the table in the outer join is allowed to have "missing" records. 对于outer join它当然很重要,因为允许outer join的表具有“缺失”记录。

It does not matter for inner joins: the optimizer will figure out the proper sequence of reading the tables, regardless of your choice for the ordering. 内部连接无关紧要:优化器将确定读取表的正确顺序,无论您选择何种顺序。

For directional outer joins, it does matter, because these are not symmetric. 对于定向外连接,它确实很重要,因为它们不对称。 You choose the table in which you want to keep all rows for the first FROM table in a left outer join; 您可以选择要在左外连接中保留第一个FROM表的所​​有行的表; for the right outer join it is the other way around. 对于右外连接,它是另一种方式。

For full outer joins it does not matter again, because the tables in full outer joins are used symmetrically to each other. 对于完全外部连接,它无关紧要,因为完全外部连接中的表彼此对称使用。

In situations when ordering does not matter you pick the order to be "natural" to the reader of your SQL statement, whatever that means for your model. 在订购无关紧要的情况下,您选择的顺序对于SQL语句的读者来说是“自然的”,无论这对您的模型意味着什么。 SQL queries very quickly become rather hard to read, so the proper ordering of your tables is important for human readers of your queries. SQL查询很快变得难以阅读,因此对于查询的人类读者来说,正确的表排序非常重要。

Well in your current example the from operator can be applied on both tables. 在您当前的示例中,from运算符可以应用于两个表。

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers,Orders
WHERE Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

->Will work like your code - >会像你的代码一样工作

The comma will join the two tables. 逗号将加入两个表。

From just means which table you are retrieving data from. 从只是意味着您从哪个表中检索数据。

In your example, you joined the two tables using different syntax. 在您的示例中,您使用不同的语法加入了两个表。 it could also have been : 它也可能是:

SELECT Customers.CustomerName, Orders.OrderID
FROM Orders
INNER JOIN Customers
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

all the code written will generate same results 所有编写的代码都会生成相同的结果

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

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