2016-10-10 22 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));

接到了我的教授

相關問題