繁体   English   中英

3个表之间的左外部联接

[英]Left outer join between 3 tables

我有3张桌子。

  1. 座位表
  2. 当地员工
  3. 外籍员工。

Seat和Local之间有一共同的列,person_id告诉哪个人坐在哪个座位上。

在Seat和Foreign表之间,也使用person_id。

我可以使用左外部联接在空位和空位以及空位的个人身份中获取席位和本地...以及席位和外部的所需数据。

我的问题是,我能否同时获得一个包含两个表的查询映射席位表并获得合并的报告?

现在正在使用的查询是:

select seat.apeh05_person_id_k
      ,seat.apeh18_seat_r seatNo
      , seat.apeh17_floor_k  seatFloor
      ,vendor.apeh15_cds_d cdsid
      , vendor.apeh15_first_n firstname
      , vendor.apeh15_last_n lastname
      ,vendor.apeh15_supervisor_cds_d ll6cdsid
      ,vendor.apeh15_ll5_cds_d ll5cdsid
      , vendor.apeh15_ll4_cds_d ll4cdsid 
 from iapeh18_seat seat ,
       IAPEH15_VENDOR_EMPLOYEE  vendor
 where seat.apeh05_person_id_k = vendor.apeh15_vendor_employee_k (+) 
 order by   seat.apeh05_person_id_k asc

另一个查询是:

select seat.apeh05_person_id_k
      ,seat.apeh18_seat_r seatNo
      , seat.apeh17_floor_k  seatFloor
      ,local.apeh09_cds_d cdsid
      ,local.apeh09_first_n firstname
      , local.apeh09_last_n lastname
      ,local.apeh09_supervisor_cds_d ll6cdsid
      ,local.apeh09_ll5_cds_d ll5cdsid
      ,  local.apeh09_ll4_cds_d ll4cdsid 
 from iapeh18_seat seat 
        , IAPEH09_LOCAL_EMPLOYEE local
 where seat.apeh05_person_id_k = local.apeh05_candidate_k (+)
 order by seat.apeh05_person_id_k asc

Remko Jansen的答案可能更有效

select seat.apeh05_person_id_k person_id
      ,seat.apeh18_seat_r seatNo
      ,seat.apeh17_floor_k  seatFloor
      ,employee.apeh15_cds_d cdsid
      ,employee.apeh15_first_n firstname
      ,employee.apeh15_last_n lastname
      ,employee.apeh15_supervisor_cds_d ll6cdsid
      ,employee.apeh15_ll5_cds_d ll5cdsid
      ,employee.apeh15_ll4_cds_d ll4cdsid 
  from iapeh18_seat seat ,
    (select * from IAPEH15_VENDOR_EMPLOYEE  
      union all
     select * from IAPEH09_LOCAL_EMPLOYEE
    ) employee
   where seat.apeh05_person_id_k = employee.apeh05_candidate_k (+)
   order by apeh05_person_id_k

首先,在进行合并和联接之后,省去了进行不同行的麻烦(相当昂贵的操作)。

您可以像这样,但是我不确定这是否是您想要的。 如果本地和供应商在apeh05_person_id_k中都有一个标识为id的人,您想怎么办?

另外,您通常需要外键约束,现在这是不可能的,因为seat.apeh05_person_id_k可以包含两个表中的ID。

总而言之,我认为这是您所要的,但我认为您的设计存在缺陷。

select 
  seat.*, -- Omitted field list for readability
  nvl2(local.apeh05_candidate_k, local.WhateverField, vendor.WhateverField) as WhateverField,
  nvl2(local.apeh05_candidate_k, local.YetAnotherField, vendor.YetAnotherField) as YetAnotherField
from 
  iapeh18_seat seat
  LEFT JOIN IAPEH15_VENDOR_EMPLOYEE vendor
    ON seat.apeh05_person_id_k = vendor.apeh15_vendor_employee_k
  LEFT JOIN IAPEH09_LOCAL_EMPLOYEE local
    ON seat.apeh05_person_id_k = local.apeh05_candidate_k
order by   
  seat.apeh05_person_id_k asc

由于两个查询都包含完全相同的列,因此可以将它们与UNION语句合并在一起,如下所示:

select seat.apeh05_person_id_k person_id
      ,seat.apeh18_seat_r seatNo
      ,seat.apeh17_floor_k  seatFloor
      ,vendor.apeh15_cds_d cdsid
      ,vendor.apeh15_first_n firstname
      ,vendor.apeh15_last_n lastname
      ,vendor.apeh15_supervisor_cds_d ll6cdsid
      ,vendor.apeh15_ll5_cds_d ll5cdsid
      ,vendor.apeh15_ll4_cds_d ll4cdsid 
  from iapeh18_seat seat ,
       IAPEH15_VENDOR_EMPLOYEE  vendor
 where seat.apeh05_person_id_k = vendor.apeh15_vendor_employee_k (+) 
UNION
select seat.apeh05_person_id_k person_id
      ,seat.apeh18_seat_r seatNo
      ,seat.apeh17_floor_k  seatFloor
      ,local.apeh09_cds_d cdsid
      ,local.apeh09_first_n firstname
      ,local.apeh09_last_n lastname
      ,local.apeh09_supervisor_cds_d ll6cdsid
      ,local.apeh09_ll5_cds_d ll5cdsid
      , local.apeh09_ll4_cds_d ll4cdsid 
 from iapeh18_seat seat 
        , IAPEH09_LOCAL_EMPLOYEE local
 where seat.apeh05_person_id_k = local.apeh05_candidate_k (+)
 order by person_id

UNION运算符返回出现在任一结果中的所有不同行。 请参阅: UNION [ALL],INTERSECT,减号运算符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM