Saa*_*ail 177 sql-server cross-join full-outer-join
SQL Server中的CROSS JOIN和FULL OUTER JOIN有什么区别?
他们是一样的,不是吗?请解释.什么时候会使用其中任何一个?
Kon*_*tin 27
我想在其他答案中添加一个重要方面,它以最佳方式向我解释了这个主题:
如果2个连接表包含M和N行,则交叉连接将始终产生(M x N)行,但完全外连接将从MAX(M,N)到(M + N)行产生(取决于实际的行数)匹配"on"谓词).
编辑:
从逻辑查询处理的角度来看,CROSS JOIN确实总是产生M×N行.FULL OUTER JOIN会发生什么,左表和右表都被"保留",好像左和右连接都发生了.因此,左表和右表中不满足ON谓词的行将添加到结果集中.
rio*_*rio 15
除了返回的 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
*/
Run Code Online (Sandbox Code Playgroud)
Kul*_*MCA 14
交叉连接:交叉连接生成的结果包含来自两个或多个表的每个行的组合.这意味着如果表A有3行而表B有2行,则CROSS JOIN将产生6行.这两个表之间没有建立任何关系 - 你实际上只是产生了所有可能的组合.
全外连接:完全外连接既不"左"也不"右" - 这两者都是!它包括参与JOIN的两个表或结果集中的所有行.当JOIN的"左"侧的行不存在匹配的行时,您会在"右侧"的结果集中看到Null值.相反,当JOIN的"右"侧的行不存在匹配行时,您会在"左侧"的结果集中看到Null值.
Chi*_*rag 14
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.
FULL OUTER JOIN
(or any other JOIN) always returns result set with less than or equal toCartesian Product number
.Number of rows returned by
FULL OUTER JOIN
equal to (No. of Rows byLEFT OUTER JOIN
) + (No. of Rows byRIGHT OUTER JOIN
) - (No. of Rows byINNER JOIN
).
小智 6
SQL 全外连接
FULL OUTER JOIN 返回左表 (table1) 和右表 (table2) 中的所有行,无论是否匹配。
FULL OUTER JOIN 关键字组合了 LEFT OUTER JOIN 和 RIGHT OUTER JOIN 的结果
参考: http: //datasciencemadesimple.com/sql-full-outer-join/
SQL 交叉连接
在 SQL CROSS JOIN 中,第一个表的每一行都与第二个表的每一行进行映射。
CROSS JOIN 操作结果集产生的行数等于第一个表中的行数乘以第二个表中的行数。
CROSS JOIN 也称为笛卡尔积/笛卡尔连接
表 A 中的行数为 m,表 B 中的行数为 n,结果表将有 m*n 行
参考: http: //datasciencemadesimple.com/sql-cross-join/
交叉加入:http://www.dba-oracle.com/t_garmany_9_sql_cross_join.htm
TLDR:生成2个表之间的所有可能组合(Carthesian产品)
(完整)外部加入:http://www.w3schools.com/Sql/sql_join_full.asp
TLDR:返回bot表中的每一行,并匹配具有相同值的结果