Oracle 外连接

Oracle outer joins

当我从 table a.

运行 时返回了两条记录

select * 从哪里 a.id = '123'

然后我创建了一个临时文件table t6,注意:在这个临时文件table中没有返回a.id 123的记录。我在 table a 和 t6 上使用 where 子句 a.id 123 进行了左连接,并期望返回 2 条记录,其中 table 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'

将您的 LEFT JOIN 更改为 RIGHT JOIN

或者改变JOIN链的方向:

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

如果我理解 main table 是 a,那么你必须在第一个位置写 table 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'

Table 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'