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

如果其他人为空,有什么方法可以获得一些结果?

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

3 回答 3

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
于 2013-03-15T21:24:10.917 回答
1
select 
  fruit, asize, product, quantity
from 
  testTBLA
  full join testTBLB using(code)
where
  code = 16

小提琴

于 2013-03-15T21:26:54.890 回答
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 Fiddle:http ://sqlfiddle.com/#!4/8b70a/2

于 2013-03-15T21:39:06.257 回答