SQL获得仅限主管的列表

时间:2018-03-05 04:12:43

标签: mysql sql

我正在尝试选择一名主管员工名单。

员工表:

+--------+----------+-----------+------------+----------+-----------------+---------+--------------------+--------------------+
| emp_id | fname    | lname     | start_date | end_date | superior_emp_id | dept_id | title              | assigned_branch_id |
+--------+----------+-----------+------------+----------+-----------------+---------+--------------------+--------------------+
|      1 | Michael  | Smith     | 2005-06-22 | NULL     |            NULL |       3 | President          |                  1 |
|      2 | Susan    | Barker    | 2006-09-12 | NULL     |               1 |       3 | Vice President     |                  1 |
|      3 | Robert   | Tyler     | 2005-02-09 | NULL     |               1 |       3 | Treasurer          |                  1 |
|      4 | Susan    | Hawthorne | 2006-04-24 | NULL     |               3 |       1 | Operations Manager |                  1 |
|      5 | John     | Gooding   | 2007-11-14 | NULL     |               4 |       2 | Loan Manager       |                  1 |
|      6 | Helen    | Fleming   | 2008-03-17 | NULL     |               4 |       1 | Head Teller        |                  1 |
|      7 | Chris    | Tucker    | 2008-09-15 | NULL     |               6 |       1 | Teller             |                  1 |
|      8 | Sarah    | Parker    | 2006-12-02 | NULL     |               6 |       1 | Teller             |                  1 |
|      9 | Jane     | Grossman  | 2006-05-03 | NULL     |               6 |       1 | Teller             |                  1 |
|     10 | Paula    | Roberts   | 2006-07-27 | NULL     |               4 |       1 | Head Teller        |                  2 |
|     11 | Thomas   | Ziegler   | 2004-10-23 | NULL     |              10 |       1 | Teller             |                  2 |
|     12 | Samantha | Jameson   | 2007-01-08 | NULL     |              10 |       1 | Teller             |                  2 |
|     13 | John     | Blake     | 2004-05-11 | NULL     |               4 |       1 | Head Teller        |                  3 |
|     14 | Cindy    | Mason     | 2006-08-09 | NULL     |              13 |       1 | Teller             |                  3 |
|     15 | Frank    | Portman   | 2007-04-01 | NULL     |              13 |       1 | Teller             |                  3 |
|     16 | Theresa  | Markham   | 2005-03-15 | NULL     |               4 |       1 | Head Teller        |                  4 |
|     17 | Beth     | Fowler    | 2006-06-29 | NULL     |              16 |       1 | Teller             |                  4 |
|     18 | Rick     | Tulman    | 2006-12-12 | NULL     |              16 |       1 | Teller             |                  4 |
+--------+----------+-----------+------------+----------+-----------------+---------+--------------------+--------------------+

我可以通过此查询验证谁是主管: SELECT DISTINCT superior_emp_id FROM employee

我试图选择一个只有主管的列表:

SELECT e.fname, e.lname, e.emp_id
FROM employee e
LEFT OUTER JOIN employee e2
    ON e.superior_emp_id = e2.emp_id
ORDER BY e.fname

但我得不到我想要的东西:

+----------+-----------+--------+
| fname    | lname     | emp_id |
+----------+-----------+--------+
| Beth     | Fowler    |     17 |
| Chris    | Tucker    |      7 |
| Cindy    | Mason     |     14 |
| Frank    | Portman   |     15 |
| Helen    | Fleming   |      6 |
| Jane     | Grossman  |      9 |
| John     | Gooding   |      5 |
| John     | Blake     |     13 |
| Michael  | Smith     |      1 |
| Paula    | Roberts   |     10 |
| Rick     | Tulman    |     18 |
| Robert   | Tyler     |      3 |
| Samantha | Jameson   |     12 |
| Sarah    | Parker    |      8 |
| Susan    | Hawthorne |      4 |
| Susan    | Barker    |      2 |
| Theresa  | Markham   |     16 |
| Thomas   | Ziegler   |     11 |
+----------+-----------+--------+

2 个答案:

答案 0 :(得分:1)

好的,如果员工的ID出现在Employee.superior_emp_id列中,那么员工就被定义为主管。如果Employee中至少有一行的ID在superior_emp_id列中,我们可以稍微重新定义此定义,并说员工是主管。这可以通过EXISTS查询来完成,并转换为以下SQL:

SELECT e.fname, e.lname, e.emp_id
FROM Employee AS e
WHERE EXISTS (
    SELECT 1
    FROM Employee e2
    WHERE e2.superior_emp_id = e.emp_id
)

附注:您实际上可以使用第一个语句编写查询。但是,有些优化器使用IN关键字非常困难 - 或者更糟糕的是如果涉及NULL - 并且通常我尽可能避免使用此关键字。为了完整性,虽然第一个陈述将转换为:

SELECT e.fname, e.lname, e.emp_id
FROM Employee AS e
WHERE e.emp_id IN (SELECT superior_emp_id FROM employee)

另外:the self join suggested by @YogeshSharma也可以使用,前提是您选择了上级(e1)的列而不是e中的员工。然而,这将为每个人产生多行,确切地说,每个人对应一个下行。可以使用DISTINCT关键字或使用上述查询来缓解此问题。

答案 1 :(得分:0)

尝试此查询

Select first_name, superior_emp_id, last_name from table where id= superior_emp_id

替换列名称,它将帮助您获得预期的数据