左联,右联与全外联【待完善】

内      联:等同于两个字段相等。结果集为两表交集;

左()外联:指一方无数据但要显示全部记录;

全  外  联:指两方都有可能没有数据,但全显示。

[Q]怎么实现左联,右联与全外联 
[A]在9i以前可以这么写: 
左联: 
select   a.id,a.name,b.address   from   a,b   
where   a.id=b.id(+) 
右联: 
select   a.id,a.name,b.address   from   a,b   
where   a.id(+)=b.id 
外联 
SELECT   a.id,a.name,b.address 
FROM   a,b 
WHERE   a.id   =   b.id(+) 
UNION 
SELECT   b.id, ' '   name,b.address 
FROM   b 
WHERE   NOT   EXISTS   ( 
SELECT   *   FROM   a 
WHERE   a.id   =   b.id); 
在9i以上,已经开始支持SQL99标准,所以,以上语句可以写成: 
默认内部联结: 
select   a.id,a.name,b.address,c.subject 
from   (a   inner   join   b   on   a.id=b.id)   
inner   join   c   on   b.name   =   c.name 
where   other_clause 
左联 
select   a.id,a.name,b.address 
from   a   left   outer   join   b   on   a.id=b.id   
where   other_clause 
右联 
select   a.id,a.name,b.address 
from   a   right   outer   join   b   on   a.id=b.id   
where   other_clause 
外联 
select   a.id,a.name,b.address 
from   a   full   outer   join   b   on   a.id=b.id   
where   other_clause 
or 
select   a.id,a.name,b.address 
from   a   full   outer   join   b   using   (id) 
where   other_clause 

猜你喜欢

转载自zlhroar.iteye.com/blog/2060107