2013-03-15 67 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