Oracle多子查询返回全部或全部

时间:2013-03-15 21:16:29

标签: sql oracle subquery

我正在发出一个包含多个子查询的查询。如果两个子查询中的一个失败,则查询不返回任何行。任何回归的唯一方法是两个查询都成功。如果另一个子查询失败,有没有办法获得成功的子查询的结果?我已经在查询的顶部以及子查询中尝试了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);

如果其他人为空,可以获得一些结果吗?

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

fiddle

答案 2 :(得分: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