通过将值与另一个表

时间:2017-11-30 00:10:44

标签: sql oracle

我有2张表格如下

employees(id int,name varchar(20),salary int);

employee_manager(eid int,mid int);

enter image description here

enter image description here

我希望得到结果

ID,NAME,Manager_Name

我只能获取Manager_Name的ID,如何获得结果。

查询获取姓名,员工ID,经理ID(员工ID)

select e.name, e.id, NVL(m.mid, 'NONE') AS Manager_ID
from employees e
left join EMPLOYEE_MANAGER m ON e.id = m.eid;

查询选择作为经理的员工姓名。

select distinct e.name 
from employees e, EMPLOYEE_MANAGER m
where m.mid = e.id;

1 个答案:

答案 0 :(得分:0)

您可以通过在连接employee_manageremployees的派生表上保持联接来获取经理信息来执行此操作:

select e.id, e.name, nvl(m.name, 'NONE') as manager_name
  from employees e
  left join (
    select em.eid, m.name
      from employee_manager em
      join employees m
        on m.id = em.mid
  ) m on m.eid = e.id