SQL Server:CROSS JOIN 和 FULL OUTER JOIN 之间有什么区别?

发布于 2024-09-09 05:56:31 字数 95 浏览 11 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

习ぎ惯性依靠 2024-09-16 05:56:31

CROSS JOIN 会在两个表之间生成笛卡尔积,返回所有行的所有可能组合。它没有 ON 子句,因为您只是将所有内容连接到所有内容。

FULL OUTER JOINLEFT OUTERRIGHT 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 no ON clause because you're just joining everything to everything.

A FULL OUTER JOIN is a combination of a LEFT OUTER and RIGHT OUTER join. It returns all rows in both tables that match the query's WHERE clause, and in cases where the ON condition can't be satisfied for those rows it puts NULL 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.

花间憩 2024-09-16 05:56:31

对于某些人来说可能并不总是显而易见的一件事是,与空表(或结果集)的交叉联接会导致空表(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.

豆芽 2024-09-16 05:56:31

我想在其他答案中添加一个重要的方面,这实际上以最好的方式向我解释了这个主题:

如果 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.

¢蛋碎的人ぎ生 2024-09-16 05:56:31

对于 SQL Server,CROSS JOIN 和 FULL OUTER JOIN 是不同的。
CROSS JOIN 只是两个表的笛卡尔积,无论任何过滤条件或任何条件如何。

FULL OUTER JOIN 给出两个表的 LEFT OUTER JOIN 和 RIGHT OUTER JOIN 的唯一结果集。它还需要 ON 子句来映射表的两列。

表 1 包含 10 行,表 2 包含 20 行,其中 5 行与特定列匹配。

那么CROSS JOIN将在结果集中返回10*20=200行。

FULL OUTER JOIN 将在结果集中返回 25 行。
INNER JOIN 将返回匹配的行,因此结果集中有 5 行。

FULL OUTER JOIN(或任何其他 JOIN)始终返回小于或等于笛卡尔积编号的结果集。

FULL OUTER JOIN 返回的行数等于(LEFT OUTER JOIN 的行数)+(RIGHT OUTER JOIN 的行数)) - (INNER JOIN 的行数)。

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 of LEFT OUTER JOIN and RIGHT OUTER JOIN of two tables. It also needs ON clause to map two columns of tables.

Table 1 contains 10 rows and Table 2 contains 20 rows with 5 rows matching on specific columns.

Then CROSS JOIN will return 10*20=200 rows in result set.

FULL OUTER JOIN will return 25 rows in result set.
INNER JOIN will return matching rows, hence, 5 rows in result set.

FULL OUTER JOIN (or any other JOIN) always returns result set with less than or equal to Cartesian Product number.

Number of rows returned by FULL OUTER JOIN equal to (No. of Rows by LEFT OUTER JOIN) + (No. of Rows by RIGHT OUTER JOIN) - (No. of Rows by INNER JOIN).

倾城月光淡如水﹏ 2024-09-16 05:56:31

除了返回 NULL 值之外,它们是相同的概念。

见下文:

declare @table1 table( col1 int, col2 int );
insert into @table1 select 1, 11 union all select 2, 22;

declare @table2 table ( col1 int, col2 int );
insert into @table2 select 10, 101 union all select 2, 202;

select
    t1.*,
    t2.*
from @table1 t1
full outer join @table2 t2 on t1.col1 = t2.col1
order by t1.col1, t2.col1;

/* full outer join
col1        col2        col1        col2
----------- ----------- ----------- -----------
NULL        NULL        10          101
1           11          NULL        NULL
2           22          2           202
*/

select
    t1.*,
    t2.*
from @table1 t1
cross join @table2 t2
order by t1.col1, t2.col1;

/* cross join
col1        col2        col1        col2
----------- ----------- ----------- -----------
1           11          2           202
1           11          10          101
2           22          2           202
2           22          10          101
*/

They are the same concepts, apart from the NULL value returned.

See below:

declare @table1 table( col1 int, col2 int );
insert into @table1 select 1, 11 union all select 2, 22;

declare @table2 table ( col1 int, col2 int );
insert into @table2 select 10, 101 union all select 2, 202;

select
    t1.*,
    t2.*
from @table1 t1
full outer join @table2 t2 on t1.col1 = t2.col1
order by t1.col1, t2.col1;

/* full outer join
col1        col2        col1        col2
----------- ----------- ----------- -----------
NULL        NULL        10          101
1           11          NULL        NULL
2           22          2           202
*/

select
    t1.*,
    t2.*
from @table1 t1
cross join @table2 t2
order by t1.col1, t2.col1;

/* cross join
col1        col2        col1        col2
----------- ----------- ----------- -----------
1           11          2           202
1           11          10          101
2           22          2           202
2           22          10          101
*/
凡尘雨 2024-09-16 05:56:31

交叉联接:交叉联接产生的结果由两个或多个表中的行的每种组合组成。这意味着如果表 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."

秋千易 2024-09-16 05:56:31

交叉连接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)

爱格式化 2024-09-16 05:56:31

SQL FULL OUTER JOIN

  • FULL OUTER JOIN 返回左表 (table1) 和右表 (table2) 中的所有行,无论是否匹配。

  • FULL OUTER JOIN 关键字组合了 LEFT OUTER JOIN 和 RIGHT OUTER JOIN 的结果

  • SQL 完全外连接也称为 FULL连接

参考: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

  • SQL full outer join is also known as FULL 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/

无声无音无过去 2024-09-16 05:56:31

完整外连接结合了左外连接和右外连接。结果集返回两个表中满足条件的行,但返回不匹配的空列。

交叉联接是一种笛卡尔积,不需要任何条件即可联接表。结果集包含由两个表相乘的行和列。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文