sql中多个连接的语法

时间:2013-10-10 20:49:33

标签: sql oracle syntax

使用Oracle:我正在尝试使用where子句进行内部自连接,然后获取该结果并在其上执行左外连接:

(select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id

不知何故,我在语法上受到挑战,无法完成这项工作。似乎无法在任何地方找到正确的语法。

2 个答案:

答案 0 :(得分:1)

将where子句放在最后。数据库将是正确的:

select * 
from table1 A
inner join table1 B on A.id = B.id
left join table2 D on D.id = A.id
where A.id is not null

在这种情况下,我们可以利用逻辑transitive property作为id列连接和where子句。

答案 1 :(得分:0)

您的第二次加入需要加入查询,在开头添加从*

select * from (select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id