我想从3个表中获取数据

时间:2019-05-31 07:02:48

标签: sql

我有3个表,分别是员工,职位和部门,

-------------------    ----------------    ------------------
| employee        |    |    jobs      |    |   department    |
-------------------    ----------------    ------------------
| empId           |    | jobId        |    |  deptId         |
| fname           |    | jobTitle     |    |  deptName       |     
| lname           |    |              |    |  managerId      |
| jobId           |    |              |    |                 |   
| managerId       |    |              |    |                 |  
| departmentId    |    |              |    |                 |  
-------------------    ----------------    -------------------

我想从员工中选择所有数据,通过jobId选择职位,通过deptId选择deptName,通过managerId和employeeId选择经理名称

SELECT EMPLOYEES.EMPLOYEE_ID, EMPLOYEES.FIRST_NAME, EMPLOYEES.LAST_NAME,
EMPLOYEES.JOB_ID, JOBS.JOB_TITLE AS JOB_TITLE, EMPLOYEES.SALARY, 
DEPARTMENTS.DEPARTMENT_ID, DEPARTMENTS.DEPARTMENT_NAME AS DEPARTMENT_NAME
FROM EMPLOYEES
LEFT JOIN JOBS ON EMPLOYEES.JOB_ID = JOBS.JOB_ID
LEFT JOIN DEPARTMENTS ON EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID

我应该怎么做才能使用empId获得管理器的名称? 还是有其他方法可以简化此代码块?

3 个答案:

答案 0 :(得分:0)

只需将另一个LEFT JOIN添加到EMPLOYEES表中,但给它起一个别名即可,例如MANAGERS。

SELECT
    EMPLOYEES.EMPLOYEE_ID,
    EMPLOYEES.FIRST_NAME,
    EMPLOYEES.LAST_NAME,
    EMPLOYEES.JOB_ID,
    JOBS.JOB_TITLE AS JOB_TITLE, 
    EMPLOYEES.SALARY, 
    DEPARTMENTS.DEPARTMENT_ID, 
    DEPARTMENTS.DEPARTMENT_NAME AS DEPARTMENT_NAME,
    MANAGERS.FIRST_NAME AS MANAGER_FIRST_NAME,
    MANAGERS.LAST_NAME AS MANAGER_LAST_NAME
FROM EMPLOYEES
LEFT JOIN EMPLOYEES MANAGERS
    ON EMPLOYEES.MANAGER_ID = MANAGERS.EMPLOYEE_ID
LEFT JOIN JOBS
    ON EMPLOYEES.JOB_ID = JOBS.JOB_ID
LEFT JOIN DEPARTMENTS
    ON EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID  

答案 1 :(得分:0)

尝试使用别名,然后使用经理ID重新加入员工。

SELECT
  E.EMPLOYEE_ID, E.FIRST_NAME, E.LAST_NAME,
  E.JOB_ID, J.JOB_TITLE AS JOB_TITLE, E.SALARY, 
  D.DEPARTMENT_ID, D.DEPARTMENT_NAME AS DEPARTMENT_NAME,
  M.EMPLOYEE_ID as mgr_id, M.FIRST_NAME as mgr_name, M.LAST_NAME as mgr_lname
FROM EMPLOYEES E LEFT JOIN JOBS J ON
        E.JOB_ID = J.JOB_ID
    LEFT JOIN DEPARTMENTS D ON
        E.DEPARTMENT_ID = D.DEPARTMENT_ID
    join employees M ON
        E.manager_ID = M.employee_ID

该查询起作用的原因是因为我正在从您已经建立的主要结果集中进行自我联接回Employees表。简单地说,假设您制作了employees表的精确副本,并将其命名为M。如果您使用原始查询并使用原始查询的Employee.manager_id到M表中的employee_ID联接到M表,那么您将有员工的经理。

没有必要创建雇员表的精确副本来查找经理。我们可以再次引用employee表,并使用一个别名(我用M作为经理)。然后,我们使用manager_id从您的员工列表中加入,以获取该员工的经理。

如果需要,您可以再次执行此操作以获取经理的经理。这是该查询:

SELECT
  E.EMPLOYEE_ID, E.FIRST_NAME, E.LAST_NAME,
  E.JOB_ID, J.JOB_TITLE AS JOB_TITLE, E.SALARY, 
  D.DEPARTMENT_ID, D.DEPARTMENT_NAME AS DEPARTMENT_NAME,
  M.EMPLOYEE_ID as mgr_id, M.FIRST_NAME as mgr_name, M.LAST_NAME as mgr_lname
FROM EMPLOYEES E LEFT JOIN JOBS J ON
        E.JOB_ID = J.JOB_ID
    LEFT JOIN DEPARTMENTS D ON
        E.DEPARTMENT_ID = D.DEPARTMENT_ID
    join employees M ON  /* The employee's manager */
        E.manager_ID = M.employee_ID
    LEFT join employees MM ON /* The employee's manager's manager */
        M.manager_ID = MM.employee_ID

我在最后一个连接中使用了左联接,因为在某些时候您将到达管理层次结构的顶部,并且可能会发现没有更多的经理。您还可以在join employees M

上添加左联接

此处为表格形式

Employee_id   |   Name     |   manager_id
     1        | Fred       |       10
     2        | Jane       |       10
    10        | Bob        |       20
    20        | Betty      |       Null

查看1号员工。E.employee_id = 1,E.Name = Fred,E.manager_id = 10。

因此查询的相关行评估如下:

    ...
    join employees M ON  /* The employee's manager */
        E.manager_ID /* i.e. 10 */ = M.employee_ID
    ...

因此,M别名现在引用 employee 记录,其中M.employee_ID = 10,因此,M.Name = Bob和M.employee_id = 20。

使用查询的最新版本,我们可以得出弗雷德的经理的经理(即弗雷德的经理是鲍勃,鲍勃的经理是贝蒂)的employee_id为20(即M.manager_ID = 20),因此MM .employee_id为20,因此是指似乎没有经理的贝蒂。

答案 2 :(得分:0)

您可以在员工表上使用另一个左联接来获取经理姓名。

SELECT (
EMPLOYEES.EMPLOYEE_ID, 
EMPLOYEES.FIRST_NAME, 
EMPLOYEES.LAST_NAME, 
EMPLOYEES.JOB_ID, 
JOBS.JOB_TITLE AS JOB_TITLE, 
EMPLOYEES.SALARY, 
DEPARTMENTS.DEPARTMENT_ID, 
DEPARTMENTS.DEPARTMENT_NAME AS DEPARTMENT_NAME,
(MGR.FIRST_NAME + ' ' + MGR.LAST_NAME) AS MANAGER_NAME) 
FROM EMPLOYEES 
LEFT JOIN JOBS 
ON EMPLOYEES.JOB_ID = JOBS.JOB_ID 
LEFT JOIN DEPARTMENTS 
ON EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
LEFT JOIN EMPLOYEES MGR
ON EMPLOYEES.MANAGERID = MGR.EMPLOYEE_ID