2013-03-15 2 views
0

여러 개의 하위 쿼리를 사용하여 쿼리를 실행하고 있습니다. 두 하위 쿼리 중 하나가 실패하면 쿼리는 행을 반환하지 않습니다. 아무것도 반환하지 않는 유일한 방법은 두 쿼리가 성공하는 것입니다. 다른 하위 쿼리가 실패한 경우 성공적인 하위 쿼리 결과를 얻을 수있는 방법이 있습니까? 나는 성공의 서브 쿼리뿐만 아니라 쿼리의 상단에있는 NVL을 시도했습니다. 스칼라 하위 쿼리가 null을 반환한다는 것을 알고 있지만 여러 값이 필요합니다. 이것은 내가 많은 것을에서 증류 한 재현 샘플 쿼리입니다, 스키마 변경/UNION은 옵션 훨씬 더 큰 수없는 곳. (적어도 나는 그렇게 생각하지 않습니다)오라클 여러 하위 쿼리가 모두 또는 아무것도 반환하지 않습니다.

create table testTBLA(
    product VARCHAR2(10), 
    quantity NUMBER, 
    code NUMBER 
); 

INSERT INTO testTBLA VALUES ('bottle', 10,3); 
INSERT INTO testTBLA VALUES ('can', 17, 16); 

create table testTBLB(
    fruit VARCHAR2(10), 
    asize NUMBER, 
    code NUMBER 
) 

INSERT INTO testTBLB VALUES ('melon', 3, 14); 
INSERT INTO testTBLB VALUES ('apple', 5, 16); 

어떤 결과의 경우를 얻을 수있는 방법 다른 사람은 null입니까?

--say code inparam is 16 
select fruit, asize, product, quantity from 
(select product, quantity from testTBLA where code=16), 
(select fruit, asize from testTBLB where code=16) 

FRUIT  ASIZE     PRODUCT QUANTITY    
---------- ---------------------- ---------- ---------------------- 
apple  5      can  17      

--say code inparam is 3 
select fruit, asize, product, quantity from 
(select product, quantity from testTBLA where code=3), 
(select fruit, asize from testTBLB where code=3) 

FRUIT  ASIZE     PRODUCT QUANTITY    
---------- ---------------------- ---------- ---------------------- 

0 rows selected 
+0

뭘 여기서 뭐하는에 직각 인'INNER JOIN' (이다

select fruit, asize, product, quantity from (select product, quantity from testTBLA where code=3) FULL OUTER JOIN (select fruit, asize from testTBLB where code=3) ON 1=1 

여기 데이터 내 코드와 SQL 바이올린의 다음 방법은 당신을 위해 작동하는 경우

보기 너의 경우). OUTER 조인을 실험 해 볼 수 있습니다 (요구 사항을 충족하는 경우). –

답변

1
select 
    fruit, asize, product, quantity 
from 
    testTBLA 
    full join testTBLB using(code) 
where 
    code = 16 

fiddle

2

양쪽이

SQL> ed 
Wrote file afiedt.buf 

    1 select fruit, asize, product, quantity 
    2 from testTBLA a 
    3   full outer join testTBLB b on(a.code = b.code) 
    4* where coalesce(a.code,b.code) = 3 
SQL>/

FRUIT   ASIZE PRODUCT  QUANTITY 
---------- ---------- ---------- ---------- 
         bottle    10 

SQL> ed 
Wrote file afiedt.buf 

    1 select fruit, asize, product, quantity 
    2 from testTBLA a 
    3   full outer join testTBLB b on(a.code = b.code) 
    4* where coalesce(a.code,b.code) = 16 
SQL>/

FRUIT   ASIZE PRODUCT  QUANTITY 
---------- ---------- ---------- ---------- 
apple    5 can    17 
관련 문제