我们可以用左右连接的联合替换完全连接吗?为什么不?

时间:2016-11-08 08:30:54

标签: sql sql-server

我们可以用左右连接的联合替换完全连接吗?如果不是,为什么?

1 个答案:

答案 0 :(得分:4)

如果T1和T2是设置(没有重复的行),则

'是',否则答案为“否”。

create table t1 (i int);
create table t2 (i int);

insert into t1 values (1);
insert into t1 values (2);
insert into t1 values (2);

insert into t2 values (3);

完全加入

select * from t1 full join t2 on t1.i=t2.i 
order by 1,2
1   (null)
2   2
2   2
(null)  3

UNION

select * from t1 left join  t2 on t1.i=t2.i
union
select * from t1 right join t2 on t1.i=t2.i
order by 1,2
1   (null)
2   2
(null)  3  

UNION ALL

select * from t1 left join  t2 on t1.i=t2.i
union all
select * from t1 right join t2 on t1.i=t2.i
order by 1,2
1   (null)
2   2
2   2
2   2
2   2
(null)  3