查询显示部门名称经理部门名称

Query to display the the department names manager department name

我必须编写一个查询来显示 table 中的工人部门及其经理部门。

一个部门不能管理自己,所有的部门都应该和它的经理部门一起显示一次。

这是员工和部门的架构 tables:

Employees:

empno char[6]

firstname varchar[12]

lastname varchar[15]

workdept char[3]

job char[9]

Department:

deptno char[3]

deptname varchar[36]

mgrno char[6]

admrdept char [3]

location char[16]

我是不是错过了什么,因为我似乎做不到。

这是我期待的输出(工人部门和经理部门是别名):

Worker Dept.                            Manager Dept.
Administration Systems                  Development Center
Development Center                      Spiffy Computer Service
Information Center                      Spiffy Computer Service
Manufacturing Systems                   Development Center
Planning                                Spiffy Computer Service Div
Support Services                        Spiffy Computer Service Div

我已经试过了,但我找不到经理部门:

SELECT distinct d.deptname,  d.location , d.admrdept  
FROM Department d 
JOIN Employee e on d.deptno = workdept 

PS:我得到第 3 列作为部门。根据上面查询的代码,如何连接到部门名称。

根据给定的信息很难判断,但我想你想要:

select d.deptname as WorkerDept, md.deptname as ManagerDept
from Employee e
inner join Department d
   on (e.workdept = d.deptno)
inner join Department md
   on (d.admrdept = md.deptno)
where d.deptno != md.deptno