【问题标题】:Select columns from table by comparing the value with another table通过将值与另一个表进行比较来从表中选择列
【发布时间】:2017-11-30 00:10:43
【问题描述】:

我有 2 个如下表

员工(id int,name varchar(20),salary int);

employee_manager(eid int, mid int);

我想得到结果

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;

【问题讨论】:

  • eid 是员工表中的id,mid 是员工表中的id,即员工经理。
  • 你使用的是哪个数据库?
  • Oracle 12c 和在 sql developer 上运行查询

标签: sql oracle


【解决方案1】:

您可以通过左联接将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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2018-08-18
    相关资源
    最近更新 更多