繁体   English   中英

左/右联接以接收所有行

[英]Left/right join to receive all rows

我如何将两个表连接在一起以从每个表中获取所有行,然后输入NULL,而另一个则丢失。

例如:

declare @t1 table (x int)
declare @t2 table (x int)

insert into @t1 select 2
insert into @t1 select 3
insert into @t1 select 4
insert into @t1 select 5

insert into @t2 select 1
insert into @t2 select 2
insert into @t2 select 5

select * 
from @t1 t1
left join @t2 t2 on t2.x = t1.x

结果应如下所示:

t1.x    t2.x
NULL    1
2       2
3       NULL
4       NULL
5       5
select * 
from @t1 t1
full outer join @t2 t2 on t2.x = t1.x

这就像左联接,但是即使没有匹配项也将从两个表中获取所有记录,并且在没有匹配项时将输入null。

select * 
from @t1 t1
FULL OUTER join @t2 t2 on t2.x = t1.x

两个表中的所有行都以完整的外部联接返回。 SQL Server对FROM子句中指定的外部联接使用以下ISO关键字:LEFT OUTER JOIN或LEFT JOIN。 正确的外部联接或正确的联接。 完全外部联接或完全联接

暂无
暂无

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

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