SQL Server:CROSS JOIN 和 FULL OUTER JOIN 之间有什么区别?
SQL Server 中的 CROSS JOIN 和 FULL OUTER JOIN 有什么区别?
它们是相同还是不同?请解释一下。什么时候会使用其中任何一个?
What is the difference between CROSS JOIN and FULL OUTER JOIN in SQL Server?
Are they the same, or not? Please explain. When would one use either of these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
CROSS JOIN 会在两个表之间生成笛卡尔积,返回所有行的所有可能组合。它没有
ON
子句,因为您只是将所有内容连接到所有内容。FULL OUTER JOIN
是LEFT OUTER
和RIGHT OUTER
连接的组合。它返回两个表中与查询的WHERE
子句匹配的所有行,并且在这些行无法满足ON
条件的情况下,它会放置NULL< /code> 未填充字段的值。
这篇 wikipedia 文章解释了各种类型的联接,并给出了一组示例的输出示例表。
A
CROSS JOIN
produces a cartesian product between the two tables, returning all possible combinations of all rows. It has noON
clause because you're just joining everything to everything.A
FULL OUTER JOIN
is a combination of aLEFT OUTER
andRIGHT OUTER
join. It returns all rows in both tables that match the query'sWHERE
clause, and in cases where theON
condition can't be satisfied for those rows it putsNULL
values in for the unpopulated fields.This wikipedia article explains the various types of joins with examples of output given a sample set of tables.
对于某些人来说可能并不总是显而易见的一件事是,与空表(或结果集)的交叉联接会导致空表(M x N;因此 M x 0 = 0)
完整外联接将始终有行,除非两者都存在M和N为0。
One thing that might not always be obvious to some is that a cross join with an empty table (or result set) results in empty table (M x N; hence M x 0 = 0)
A full outer join will always have rows unless both M and N are 0.
我想在其他答案中添加一个重要的方面,这实际上以最好的方式向我解释了这个主题:
如果 2 个连接表包含 M 和 N 行,那么交叉连接将始终生成 (M x N) 行,但是完整的外连接将生成 MAX(M,N) 到 (M + N) 行(取决于实际匹配“on”谓词的行数)。
编辑:
从逻辑查询处理的角度来看,CROSS JOIN 确实总是产生 M x N 行。 FULL OUTER JOIN 发生的情况是左表和右表都被“保留”,就好像左连接和右连接都发生了一样。因此,左表和右表中不满足 ON 谓词的行都会添加到结果集中。
I'd like to add one important aspect to other answers, which actually explained this topic to me in the best way:
If 2 joined tables contain M and N rows, then cross join will always produce (M x N) rows, but full outer join will produce from MAX(M,N) to (M + N) rows (depending on how many rows actually match "on" predicate).
EDIT:
From logical query processing perspective, CROSS JOIN does indeed always produce M x N rows. What happens with FULL OUTER JOIN is that both left and right tables are "preserved", as if both LEFT and RIGHT join happened. So rows, not satisfying ON predicate, from both left and right tables are added to the result set.
对于 SQL Server,
CROSS JOIN 和 FULL OUTER JOIN
是不同的。CROSS JOIN
只是两个表的笛卡尔积,无论任何过滤条件或任何条件如何。FULL OUTER JOIN
给出两个表的LEFT OUTER JOIN 和 RIGHT OUTER JOIN
的唯一结果集。它还需要 ON 子句来映射表的两列。For SQL Server,
CROSS JOIN and FULL OUTER JOIN
are different.CROSS JOIN
is simply Cartesian Product of two tables, irrespective of any filter criteria or any condition.FULL OUTER JOIN
gives unique result set ofLEFT OUTER JOIN and RIGHT OUTER JOIN
of two tables. It also needs ON clause to map two columns of tables.除了返回 NULL 值之外,它们是相同的概念。
见下文:
They are the same concepts, apart from the NULL value returned.
See below:
交叉联接:交叉联接产生的结果由两个或多个表中的行的每种组合组成。这意味着如果表 A 有 3 行,表 B 有 2 行,则 CROSS JOIN 将产生 6 行。两个表之间没有建立任何关系 - 您实际上只是生成所有可能的组合。
完整外部连接:完整外部连接既不是“左”也不是“右”——它是两者!它包括参与 JOIN 的两个表或结果集中的所有行。当 JOIN“左侧”的行不存在匹配行时,您会在“右侧”的结果集中看到 Null 值。相反,当 JOIN“右侧”的行不存在匹配行时,您会在“左侧”的结果集中看到 Null 值。
Cross join :Cross Joins produce results that consist of every combination of rows from two or more tables. That means if table A has 3 rows and table B has 2 rows, a CROSS JOIN will result in 6 rows. There is no relationship established between the two tables – you literally just produce every possible combination.
Full outer Join : A FULL OUTER JOIN is neither "left" nor "right"— it's both! It includes all the rows from both of the tables or result sets participating in the JOIN. When no matching rows exist for rows on the "left" side of the JOIN, you see Null values from the result set on the "right." Conversely, when no matching rows exist for rows on the "right" side of the JOIN, you see Null values from the result set on the "left."
交叉连接:http://www.dba-oracle.com/ t_garmany_9_sql_cross_join.htm
TLDR; 生成 2 个表之间所有可能的组合(笛卡尔积)
(完整)外连接:http://www.w3schools.com/Sql/sql_join_full.asp
TLDR; 返回每一行在两个表中以及具有相同值的结果(条件中的匹配)
Cross Join: http://www.dba-oracle.com/t_garmany_9_sql_cross_join.htm
TLDR; Generates a all possible combinations between 2 tables (Carthesian product)
(Full) Outer Join: http://www.w3schools.com/Sql/sql_join_full.asp
TLDR; Returns every row in both tables and also results that have the same values (matches in CONDITION)
SQL FULL OUTER JOIN
FULL OUTER JOIN 返回左表 (table1) 和右表 (table2) 中的所有行,无论是否匹配。
FULL OUTER JOIN 关键字组合了 LEFT OUTER JOIN 和 RIGHT OUTER JOIN 的结果
参考:http://datasciencemadesimple.com/sql-full-outer-join/
SQL CROSS JOIN
在 SQL CROSS JOIN 中,第一个表的每一行都映射到第二个表的每一行。
CROSS JOIN 操作结果集产生的行数等于第一个表的行数乘以第二个表的行数。
CROSS JOIN 也称为笛卡尔积/笛卡尔联接
表 A 中的行数为 m,表 B 中的行数为 n,结果表将有 m*n 行
参考:http://datasciencemadesimple.com/sql-cross-join/
SQL FULL OUTER JOIN
The FULL OUTER JOIN returns all rows from the left table (table1) and from the right table (table2) irrespective of the match.
The FULL OUTER JOIN keyword combines the result of both LEFT OUTER JOIN and RIGHT OUTER JOIN
Reference : http://datasciencemadesimple.com/sql-full-outer-join/
SQL CROSS JOIN
In SQL CROSS JOIN Each Row of first table is mapped with the each and every row of second table.
Number of rows produced by a result set of CROSS JOIN operation is equal to number of rows in the first table multiplied by the number of rows in the second table.
CROSS JOIN is also known as Cartesian product / Cartesian join
Number of rows in table A is m, Number of rows in table B is n and resultant table will have m*n rows
Reference:http://datasciencemadesimple.com/sql-cross-join/
完整外连接结合了左外连接和右外连接。结果集返回两个表中满足条件的行,但返回不匹配的空列。
交叉联接是一种笛卡尔积,不需要任何条件即可联接表。结果集包含由两个表相乘的行和列。
A full outer join combines a left outer join and a right outer join. The result set returns rows from both tables where the conditions are met but returns null columns where there is no match.
A cross join is a Cartesian product that does not require any condition to join tables. The result set contains rows and columns that are a multiplication of both tables.