2013-03-15 59 views
0

我發出一個包含多個子查詢的查詢。如果其中一個子查詢失敗,則查詢不返回任何行。如果兩個查詢都成功,返回的唯一方法就是返回。如果另一個失敗,有什麼辦法可以獲得成功的子查詢的結果嗎?我已經在查詢的頂部以及子查詢中嘗試了NVL,但沒有成功。我知道標量子查詢返回null,但我需要多個值。這是一個重複的樣品查詢我已經從很多東西蒸餾,更大的地方架構更改/ UNION是不是一種選擇。(至少我不這麼認爲)Oracle多重子查詢返回全部或全部無

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); 

任何方式得到一些結果,如果其他人是空的?

--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'(這是笛卡爾你的情況)。你可能想嘗試'OUTER'連接(如果這符合你的要求)。 – 2013-03-15 21:31:36

回答

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 
0

你的問題是不是太清楚。

看看下面的方法也適用於您:

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撥弄你的數據和我的代碼:http://sqlfiddle.com/#!4/8b70a/2