# 外连接的两种写法的总结

--无其他条件
select * from n_fwy_a a left join n_fwy_b b on a.id=b.id order by a.id;
select * from n_fwy_a a ,n_fwy_b b where a.id=b.id(+) order by a.id;

--在主表加附加条件
select * from n_fwy_a a ,n_fwy_b b where a.id=b.id(+) and a.id >5 order by a.id;
select * from n_fwy_a a left join n_fwy_b b on a.id=b.id where a.id>5 order by a.id;
select * from n_fwy_a a left join n_fwy_b b on a.id=b.id and a.id>5 order by a.id;--no

--在附表中加条件
select * from n_fwy_a a ,n_fwy_b b where a.id=b.id(+) and b.id(+) >5 order by a.id;
select * from n_fwy_a a left join n_fwy_b b on a.id=b.id where b.id >5 order by a.id;--no
select * from n_fwy_a a left join n_fwy_b b on a.id=b.id and b.id >5 order by a.id;

--对于in 和 or 操作

--对于in操作
select *
from n_fwy_a a ,
(select * from n_fwy_b b where b.id in (2,8)) b
where a.id=b.id (+);
select *
from n_fwy_a a left join n_fwy_b b on a.id=b.id and b.id in (2,8);

select *
from (select * from n_fwy_a a where a.id in (2,9)) a ,
n_fwy_b b
where a.id=b.id (+);
select *
from n_fwy_a a left join n_fwy_b b on a.id=b.id and a.id in (2,9);

－－对于OR操作与in的一致

对于(+)风格的外关联,where后的条件是关联后的筛选数据的条件。针对附表不能使用in和or操作，若要使用应该在关联之前使用，以缩小数据集合(即在from 中使用表查询)。
否则，起不到外关联的作用，结果集为内连接的结果

[@more@]

• 博文量
54
• 访问量
921147