oracle左外连接有两个表

时间:2014-10-10 22:48:16

标签: sql oracle select outer-join

我有3个表,比如:

create table KOPA (MISS VARCHAR2(12), SER NUMBER(10));
create table NOTI (MISS VARCHAR2(12), LAI NUMBER(10), BAI NUMBER(10), ZAI NUMBER(10), KAI UMBER(10));
create table DOSI (MISS VARCHAR2(12), MAI NUMBER(10));

INSERT INTO KOPA (MISS,SER) VALUES ('AAA',123);
INSERT INTO KOPA (MISS,SER) VALUES ('BBB',234);
INSERT INTO KOPA (MISS,SER) VALUES ('CCC',345);
INSERT INTO KOPA (MISS,SER) VALUES ('DDD',456);
INSERT INTO KOPA (MISS,SER) VALUES ('EEE',567);
INSERT INTO KOPA (MISS,SER) VALUES ('TTT',456);
INSERT INTO KOPA (MISS,SER) VALUES ('YYY',567);
INSERT INTO KOPA (MISS,SER) VALUES ('III',456);

INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('AAA',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('BBB',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('CCC',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('FFF',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('GGG',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('HHH',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('III',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('JJJ',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('KKK',1,2,3,NULL);
INSERT INTO NOTI (MISS,LAI,BAI,ZAI,KAI) VALUES ('LLL',1,2,3,NULL);

INSERT INTO DOSI (MISS,MAI) VALUES ('BBB',4);
INSERT INTO DOSI (MISS,MAI) VALUES ('CCC',1);
INSERT INTO DOSI (MISS,MAI) VALUES ('DDD',2);
INSERT INTO DOSI (MISS,MAI) VALUES ('EEE',3);

SELECT * FROM KOPA A;
SELECT * FROM NOTI H;
SELECT * FROM DOSI B;

我正在寻找的结果是:

a.miss a.ser h.lai h.bai h.zai b.miss b.mai
AAA    123   1     2     3     null   null
BBB    234   1     2     3     BBB    4
CCC    345   1     2     3     CCC    1
DDD    456   null  null  null  DDD    2
EEE    567   null  null  null  EEE    3
III    456   1     2     3     null   null

所以 - 问题是 - 如何用一个表获得两个外连接? 像这样:

select z.*,b.*
from (select a.miss,h.lai,h.bai from kopa a,noti h
where a.miss=h.miss and h.lai=1 and h.bai=2)z
full outer join dosi b on b.miss=z.miss

只有,有没有办法在每一行显示a.miss?

2 个答案:

答案 0 :(得分:2)

使用您的示例数据,此查询可以获得所需的结果:

select a.miss, a.ser, h.lai, h.bai, h.zai, b.miss, b.mai
from kopa a
left join noti h on a.miss = h.miss and h.lai = 1 and h.bai = 2
left join dosi b on b.miss = a.miss
where h.miss is not null or b.miss is not null
order by a.miss

输出:

miss     ser         lai         bai         zai         miss         mai
-------- ----------- ----------- ----------- ----------- ------------ -----------
AAA      123         1           2           3           NULL         NULL
BBB      234         1           2           3           BBB          4
CCC      345         1           2           3           CCC          1
DDD      456         NULL        NULL        NULL        DDD          2
EEE      567         NULL        NULL        NULL        EEE          3
III      456         1           2           3           NULL         NULL

Sample SQL Fiddle

答案 1 :(得分:1)

  

有没有办法在每一行显示a.miss?

要在每行中显示a.miss,请使用LEFT JOIN

试试这个

select a.miss as a_miss, a.ser, h.lai, h.bai, h.zai, b.miss as b_miss, b.mai
from Kopa a
left outer join noti h on h.miss=a.miss
full outer join dosi b on b.miss=a.miss 
ORDER BY a.miss

SQL小提琴http://sqlfiddle.com/#!4/92e99/9