oracle左外连接和右外连接的区别

准备测试数据

create table T_A
(
  a_id   NUMBER,
  a_name VARCHAR2(10)
);
create table T_B
(
  b_id   NUMBER,
  b_name VARCHAR2(10)
);
insert into t_b (B_ID, B_NAME) values ('1', 'AA');
insert into t_b (B_ID, B_NAME) values ('1', 'AB');
insert into t_b (B_ID, B_NAME) values ('2', 'BB');
insert into t_b (B_ID, B_NAME) values ('4', 'DD');
insert into t_a (A_ID, A_NAME) values ('1', 'A');
insert into t_a (A_ID, A_NAME) values ('2', 'B');
insert into t_a (A_ID, A_NAME) values ('3', 'C');
commit;

示例中t_A看成左表,t_B看成右表

左外连接:t_A的数据都显示,加上t_A和t_B匹配后的数据,t_B表不足的地方均为NULL。

Select a.*,b.* from t_A a,t_B b where a.A_id=b.b_id(+) order by a.a_id;
select a.*,b.* from t_A a left join t_B b on a.A_id = b.b_id order by a.a_id;

右外连接,t_B的数据都显示,加上t_A和t_B匹配后的数据。 t_A表不足的地方均为NULL。

Select  a.*,b.* from t_A a,t_B b where a.a_id(+)=b.b_id order by a.a_id;
select  a.*,b.* from t_A a right join t_B b on a.a_id = b.b_id order by a.a_id;

简单的理解为:
(+)在右,是左外连接。左表的数据全展示
(+)在左,是右外连接。右表的数据全展示

 

posted @ 2022-03-07 11:01  shiyunyier  阅读(701)  评论(0)    收藏  举报