it-source

Oracle 왼쪽 외부 조인이 오른쪽 null 값을 표시하지 않음

criticalcode 2023. 11. 7. 20:56
반응형

Oracle 왼쪽 외부 조인이 오른쪽 null 값을 표시하지 않음

Oracle에서 누락된 값에 참여하지 않으려는 쿼리를 만드는 데 문제가 있습니다.

제가 가지고 있는 테이블은 다음과 같습니다.

table myTable(refnum, contid, type)

values are:
1, 10, 90000
2, 20, 90000
3, 30, 90000
4, 20, 10000
5, 30, 10000
6, 10, 20000
7, 20, 20000
8, 30, 20000

내가 쫓는 분야의 분류는 다음과 같습니다.

select a.refnum from myTable a where type = 90000
select b.refnum from myTable b where type = 10000 and contid in (select contid from myTable where type = 90000)
select c.refnum from myTable c where type = 20000 and contid in (select contid from myTable where type = 90000)

제가 찾는 질문의 결과는 다음과 같습니다.

a.refnum, b.refnum, c.refnum

효과가 있을 거라 생각했습니다.

select a.refnum, b.refnum, c.refnum
from myTable a 
left outer join myTable b on (a.contid = b.contid) 
left outer join myTable c on (a.contid = c.contid) 
where a.id_tp_cd = 90000
and b.id_tp_cd = 10000
and c.id_tp_cd = 20000

따라서 값은 다음과 같아야 합니다.

1, null, 6
2, 4, 7
3, 5, 8

하지만 그것은 오직 돌아올 뿐입니다.

2, 4, 7
3, 5, 8

왼쪽 조인은 왼쪽의 모든 값을 표시하고 오른쪽에 대한 null을 생성할 것이라고 생각했습니다.

help :(

왼쪽 조인이 일치하지 않는 오른쪽에 대해 null을 반환한다는 것은 옳지만, where 절에 이 제한을 추가할 때는 이러한 null을 반환할 수 없도록 허용하지 않습니다.

and b.id_tp_cd = 10000
and c.id_tp_cd = 20000

대신 조인의 'on' 절에 넣을 수 있어야 하므로 오른쪽에 있는 관련 행만 반환됩니다.

select a.refnum, b.refnum, c.refnum
from myTable a 
left outer join myTable b on (a.contid = b.contid and b.id_tp_cd = 10000) 
left outer join myTable c on (a.contid = c.contid and c.id_tp_cd = 20000) 
where a.id_tp_cd = 90000

또는 ansi 대신 Oracle 구문을 사용합니다.

select a.refnum, b.refnum, c.refnum
from myTable a, mytable b, mytable c
where a.contid=b.contid(+)
and a.contid=c.contid(+)
and a.type = 90000
and b.type(+) = 10000
and c.type(+) = 20000;


REFNUM     REFNUM     REFNUM
---------- ---------- ----------
     1                     6
     2          4          7
     3          5          8

언급URL : https://stackoverflow.com/questions/362556/oracle-left-outer-joins-not-showing-right-null-values

반응형