查询以映射sql中另一个表中的列的值

时间:2016-06-08 13:38:15

标签: sql oracle-sqldeveloper

我有一张表empbu_source。现在emp表格就是

mail_id            first_name    pos      bu_name
sreekom@abc.xom    sreekom       abc      land_bu
sreekom@abc.xom    sreekom       abc      land_bu
simth @thought.com  smith        xyz      MEZC 

bu_source - 此表执行bu_name的分组。例如land_bu,aev_bu将分组到电力公司,BEZC和MEZC将分组到Equity Ventures。 表结构如下:

 source_bu_name result_bu_name 
    land_bu        power corporation
    aev_bu         power corporation
    BEZC           Equity ventures
    MEZC           Equity ventures

我想要一个查询结果如下:

mail_id            first_name    pos      bu_name result_bu_name 
sreekom@abc.xom    sreekom       abc      land_bu  power corporation
simth @thought.com  smith        xyz      MEZC      Equity ventures

我创建了一个查询

SELECT 
mail_id             ,
First_Name      ,
pos      ,
Source_Bu_Name      , -- bu name 
result_bu_name 
From Emp
left join bu_source on Emp.BU_NAME = bu_source.SOURCE_BU_NAME;

但是,尽管表中有值,但这对于bu_name很少有空列。

1 个答案:

答案 0 :(得分:0)

您需要将其更改为

SELECT 
    e.mail_id             
    ,e.first_name      
    ,e.pos      
    ,e.bu_name       --bs.source_bu_name
    ,bs.result_bu_name  
FROM 
    Emp e
LEFT JOIN 
    bu_source bs
ON 
    e.bu_name = bs.source_bu_name;