Oracle外连接

时间:2019-04-09 06:11:33

标签: sql oracle outer-join

当我从表a运行时,返回了两条记录。

从a.id ='123'的位置选择*

然后我创建了一个临时表t6,注意:此临时表中没有返回a.id 123的记录。我在其中使用a子句a.id 123的表a和t6进行了左连接,并期望返回2条记录,其中表t6中的值将为null或空白。但是它没有返回任何行。我想知道如何修复使其正常工作。

select a.*, t6.*
from 
(select t5.column1, t5.column2, t1.column3, t1.column9, t2.column9, t3.column9, t4.column9
from            (select column3, column8, column9 from t7
                where upper(column8) = upper('abcd')
                )  t1
                join (select column3, column8, column9 from t7               
                where upper(column8) = upper('efgh')
                ) t2
                on t2.column3 = t1.column3
                and t1.column3 in (select id2 from a where a.id = '123')
                left outer join
                (select column3, column8, column9 from t7
                where upper(column8) = upper('ijkl')
                ) t3
                on t3.column3 = t1.column3    
                left outer join
               (select column3, column8, column9 from t7
                where upper(column8) = upper('mnop')
                ) t4
                on t4.column3 = t1.column3
                join t5  
                on t1.column9 =  t5.column10
) t6
left join
a
on a.id2 = t6.column3
where a.id = '123'

3 个答案:

答案 0 :(得分:0)

将您的LEFT JOIN更改为RIGHT JOIN

或更改JOIN链的方向:

SELECT a.*, t6.*
FROM a LEFT JOIN ( SELECT ..... ) ON a.id2 = t6.column3
WHERE a.id = '123'

答案 1 :(得分:0)

如果我了解主表是a,那么您必须像这样在第一个位置写表a:

select a.*, t6.*
from a left join
(select t5.column1, t5.column2, t1.column3, t1.column9, t2.column9, t3.column9, t4.column9
from            (select column3, column8, column9 from t7
                where upper(column8) = upper('abcd')
                )  t1
                join (select column3, column8, column9 from t7               
                where upper(column8) = upper('efgh')
                ) t2
                on t2.column3 = t1.column3
                and t1.column3 in (select id2 from a where a.id = '123')
                left outer join
                (select column3, column8, column9 from t7
                where upper(column8) = upper('ijkl')
                ) t3
                on t3.column3 = t1.column3    
                left outer join
               (select column3, column8, column9 from t7
                where upper(column8) = upper('mnop')
                ) t4
                on t4.column3 = t1.column3
                join t5  
                on t1.column9 =  t5.column10
) t6
on a.id2 = t6.column3
where a.id = '123'

答案 2 :(得分:0)

表A应该在左侧,#t6应该在右侧。

from a 
left join #t6 on on a.id2 = t6.column3

查询

select a.*, t6.*
from a
left join
(select t5.column1, t5.column2, t1.column3, t1.column9, t2.column9, t3.column9, t4.column9
from            (select column3, column8, column9 from t7
                where upper(column8) = upper('abcd')
                )  t1
                join (select column3, column8, column9 from t7               
                where upper(column8) = upper('efgh')
                ) t2
                on t2.column3 = t1.column3
                and t1.column3 in (select id2 from a where a.id = '123')
                left outer join
                (select column3, column8, column9 from t7
                where upper(column8) = upper('ijkl')
                ) t3
                on t3.column3 = t1.column3    
                left outer join
               (select column3, column8, column9 from t7
                where upper(column8) = upper('mnop')
                ) t4
                on t4.column3 = t1.column3
                join t5  
                on t1.column9 =  t5.column10
) t6

on a.id2 = t6.column3
where a.id = '123'