带有一个选定表的完全外连接

时间:2013-11-17 15:07:38

标签: sql oracle sqlplus

我的查询中有一个复杂的select语句,我使用了三次,用于外连接。我想简化它以使用复杂查询一次,但这不起作用:

select three.faculty, three.chair, one.students honors, 
  two.students normal, three.students bad
from [complex select statement] one
full outer join
select * from (one) two
on one.faculty=two.faculty and one.chair=two.chair and one.n=two.n
full outer join
select * from (one) three
on two.chair=three.chair and two.faculty=three.faculty and two.n=three.n

使用复杂查询的版本重复工作:

select three.faculty, three.chair,one.students honors,
  two.students normal, three.students bad
from [complex select statement] one
full outer join
select * from [complex select statement] two
on one.faculty=two.faculty and one.chair=two.chair and one.n=two.n
full outer join
select * from [complex select statement] three
on two.chair=three.chair and two.faculty=three.faculty and two.n=three.n

1 个答案:

答案 0 :(得分:0)

您无法加入像这样的子查询。相反,你需要使用一个公用表表达式,这样做(道歉我不完全了解Oracle的语法):

WITH results AS (SELECT lots_of_things FROM lots_of_tables)
SELECT * FROM results
FULL OUTER JOIN results ON (...)
FULL OUTER JOIN results ON (...)

实质上,CTE意味着数据库引擎将表达式的结果提供为可用于查询其余部分的表样式值(包括连接等)。 Oracle提供documentation on the WITH clause.