2016-10-10 23 views
-2

我面临这个问题,不知道该怎么做。我有以下问题。 显示公司中所有部门的名称以及其经理的姓名。您的查询必须显示公司中的所有部门,即使它没有经理。SQL查询 - 如何在公司中向经理显示部门?

我的问题是,当我运行一个查询只给我部门名称时,我得到了27个结果。但是对于经理查询,只有18个结果。

我不知道如何做到这一点。我曾经得到这个查询是,对于部门名称:

--select department_name from departments; 
Administration 
Marketing 
Purchasing 
Human Resources 
Shipping 
IT 
Public Relations 
Sales 
Executive 
Finance 
Accounting 
Treasury 
Corporate Tax 
Control And Credit 
Shareholder Services 
Benefits 
Manufacturing 
Construction 
Contracting 
Operations 
IT Support 
NOC 
IT Helpdesk 
Government Sales 
Retail Sales 
Recruiting 
Payroll 

第二部分:

select first_name, last_name, department_name from employees e inner join departments d on 
d.department_id=e.department_id where (employee_id in (select manager_id from employees)); 

first_name last_name department_name 
Steven King Executive 
Neena Kochhar Executive 
Lex De Haan Executive 
Alexander Hunold IT 
Nancy Greenberg Finance 
Den Raphaely Purchasing 
Matthew Weiss Shipping 
Adam Fripp Shipping 
Payam Kaufling Shipping 
Shanta Vollman Shipping 
Kevin Mourgos Shipping 
John Russell Sales 
Karen Partners Sales 
Alberto Errazuriz Sales 
Gerald Cambrault Sales 
Eleni Zlotkey Sales 
Michael Hartstein Marketing 
Shelley Higgins Accounting 

第二部分只与管理人员返回部门。

+1

答案假设你想学习,而不是让我们自己做功课,看http://stackoverflow.com/questions/38549/what-is-the-difference-在内部加入和外部加入 –

+0

没关系球员这是一个外部联接,我的教授清除了这个!得到它的工作 –

回答

0
select e.first_name, e.last_name, d.department_name 
from employee e right join department d 
    on e.department_id = d.department_id 
where e.employee_id in (select manager_id from employee) 
    or e.manager_id is null; 
0

对于那些有兴趣谁:

选择d.department_name为“部门名称”,e.first_name为“经理名字”,e。姓氏来自各部门的经理姓倒是 按照department_name的顺序,在外部加入员工e上d.department_id = e.department_id和(employee_id in(select manager_id from employees));

接到了我的教授

相关问题