`
fantaxy025025
  • 浏览: 1249034 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

数据库_内连接_外连接_左连接_右连接

阅读更多

总结:

 

内连接:仅仅显示匹配的行

外连接:设法显示不匹配的行,包括左(外)连接、右(外)连接、全(外)连接

左(外)连接:左表的行全显示(右表不存在匹配时填null)

右(外)连接:右表的行全显示(左表不存在匹配时填null)

全(外)连接:左连接和右连接的结果再做合并(union)

 

SQL:

 

ANSI的写法 不太好看,但是意义明显。

内连接:join 或 inner join

左(外)连接:left join

右(外)连接:right join

 

Oracle的写法 是在需要补充填null的一方添加“(+)”,表示用null去匹配另一边“没有匹配”的行。

内连接:什么也不做

左(外)连接:右边填“(+)”

右(外)连接:左边填“(+)”

 

 

练习:

--prepare

create global temporary table temp1 (
    id number(3), desc1 char(5)
);
create global temporary table temp2 (id number(3), desc2 char(5));
insert into temp1 values (123, 'ABCDE');
insert into temp1 values (456, 'FGHIJ');
insert into temp2 values (456, 'ZZZZZ');
insert into temp2 values (789, 'MMMMM');

 

Oracle

--left join

select a.id id_a, b.id id_b
from temp1 a, temp2 b
where a.id(+) = b.id

 

--right join

select a.id id_a, b.id id_b
from temp1 a, temp2 b
where a.id= b.id(+)

 

ASNI

--inner join(inner is optional )

select ta.id id_a, tb.id id_b
from temp1 ta
  inner join temp2 tb
  on ta.id = tb.id

 

--left join

select ta.id id_a, tb.id id_b
from temp1 ta
  lest join temp2 tb
  on ta.id = tb.id

 

--right join

select ta.id id_a, tb.id id_b
from temp1 ta
  right join temp2 tb
  on ta.id = tb.id

 

--Oracle全外连接(ASIN略)

select a.id id_a, b.id id_b
from temp1 a, temp2 b
where a.id(+) = b.id
      union
select a.id id_a, b.id id_b
from temp1 a, temp2 b
where a.id = b.id(+)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics