如何让员工和他们的经理在一起

发布于 2024-12-05 07:39:58 字数 2444 浏览 0 评论 0原文

这就是我想要的输出:

Employee   Emp# Manager Mgr# 
   BLAKE   7698 KING    7839 
   CLARK   7782 KING    7839 
   JONES   7566 KING    7839 
   MARTIN  7654 BLAKE   7698 
   ALLEN   7499 BLAKE   7698 
   TURNER  7844 BLAKE   7698 
   JAMES   7900 BLAKE   7698 
   WARD    7521 BLAKE   7698 
   FORD    7902 JONES   7566 
   SMITH   7369 FORD    7902 
   SCOTT   7788 JONES   7566 
   ADAMS   7876 SCOTT   7788 
   MILLER  7934 CLARK   7782 

这是我得到的:

 SQL> SELECT ename, empno, (SELECT ename FROM EMP WHERE empno = mgr)AS MANAGER, mgr from emp order by empno;

 ENAME           EMPNO MANAGER           MGR
 ---------- ---------- ---------- ----------
 SMITH            7369                  7902
 ALLEN            7499                  7698
 WARD             7521                  7698
 JONES            7566                  7839
 MARTIN           7654                  7698
 BLAKE            7698                  7839
 CLARK            7782                  7839
 SCOTT            7788                  7566
 KING             7839
 TURNER           7844                  7698
 ADAMS            7876                  7788

 ENAME           EMPNO MANAGER           MGR
 ---------- ---------- ---------- ----------
 JAMES            7900                  7698
 FORD             7902                  7566
 MILLER           7934                  7782

我找不到经理字段为何为空。

这是表格:

 SQL> select empno, ename, job,deptno, mgr from emp;

 EMPNO ENAME      JOB           DEPTNO        MGR
 ---------- ---------- --------- ---------- ----------
  7839 KING       PRESIDENT         10
  7698 BLAKE      MANAGER           30       7839
  7782 CLARK      MANAGER           10       7839
  7566 JONES      MANAGER           20       7839
  7654 MARTIN     SALESMAN          30       7698
  7499 ALLEN      SALESMAN          30       7698
  7844 TURNER     SALESMAN          30       7698
  7900 JAMES      CLERK             30       7698
  7521 WARD       SALESMAN          30       7698
  7902 FORD       ANALYST           20       7566
  7369 SMITH      CLERK             20       7902

 EMPNO ENAME      JOB           DEPTNO        MGR
 ---------- ---------- --------- ---------- ----------
  7788 SCOTT      ANALYST           20       7566
  7876 ADAMS      CLERK             20       7788
  7934 MILLER     CLERK             10       7782

选择了 14 行。

This is what I want the output to look like:

Employee   Emp# Manager Mgr# 
   BLAKE   7698 KING    7839 
   CLARK   7782 KING    7839 
   JONES   7566 KING    7839 
   MARTIN  7654 BLAKE   7698 
   ALLEN   7499 BLAKE   7698 
   TURNER  7844 BLAKE   7698 
   JAMES   7900 BLAKE   7698 
   WARD    7521 BLAKE   7698 
   FORD    7902 JONES   7566 
   SMITH   7369 FORD    7902 
   SCOTT   7788 JONES   7566 
   ADAMS   7876 SCOTT   7788 
   MILLER  7934 CLARK   7782 

Here's what I got:

 SQL> SELECT ename, empno, (SELECT ename FROM EMP WHERE empno = mgr)AS MANAGER, mgr from emp order by empno;

 ENAME           EMPNO MANAGER           MGR
 ---------- ---------- ---------- ----------
 SMITH            7369                  7902
 ALLEN            7499                  7698
 WARD             7521                  7698
 JONES            7566                  7839
 MARTIN           7654                  7698
 BLAKE            7698                  7839
 CLARK            7782                  7839
 SCOTT            7788                  7566
 KING             7839
 TURNER           7844                  7698
 ADAMS            7876                  7788

 ENAME           EMPNO MANAGER           MGR
 ---------- ---------- ---------- ----------
 JAMES            7900                  7698
 FORD             7902                  7566
 MILLER           7934                  7782

I can't find why the manager field is blank.

Here's the table:

 SQL> select empno, ename, job,deptno, mgr from emp;

 EMPNO ENAME      JOB           DEPTNO        MGR
 ---------- ---------- --------- ---------- ----------
  7839 KING       PRESIDENT         10
  7698 BLAKE      MANAGER           30       7839
  7782 CLARK      MANAGER           10       7839
  7566 JONES      MANAGER           20       7839
  7654 MARTIN     SALESMAN          30       7698
  7499 ALLEN      SALESMAN          30       7698
  7844 TURNER     SALESMAN          30       7698
  7900 JAMES      CLERK             30       7698
  7521 WARD       SALESMAN          30       7698
  7902 FORD       ANALYST           20       7566
  7369 SMITH      CLERK             20       7902

 EMPNO ENAME      JOB           DEPTNO        MGR
 ---------- ---------- --------- ---------- ----------
  7788 SCOTT      ANALYST           20       7566
  7876 ADAMS      CLERK             20       7788
  7934 MILLER     CLERK             10       7782

14 rows selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

北凤男飞 2024-12-12 07:39:58

这是一个经典的自联接,请尝试以下操作:

SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM
    emp e, emp m
WHERE e.mgr = m.empno

如果您想包含没有经理的总裁,则在 Oracle 中使用外部联接而不是内部联接语法:

SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM
    emp e, emp m
WHERE e.mgr = m.empno(+)

或者采用 ANSI SQL 语法:

SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM
    emp e
    LEFT OUTER JOIN emp m
        ON e.mgr = m.empno

This is a classic self-join, try the following:

SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM
    emp e, emp m
WHERE e.mgr = m.empno

And if you want to include the president which has no manager then instead of an inner join use an outer join in Oracle syntax:

SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM
    emp e, emp m
WHERE e.mgr = m.empno(+)

Or in ANSI SQL syntax:

SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM
    emp e
    LEFT OUTER JOIN emp m
        ON e.mgr = m.empno
仅此而已 2024-12-12 07:39:58
(SELECT ename FROM EMP WHERE empno = mgr)

EMP 中没有符合此标准的记录。

您需要自加入才能获得这种关系。

SELECT e.ename AS Employee, e.empno, m.ename AS Manager, m.empno
FROM EMP AS e LEFT OUTER JOIN EMP AS m
ON e.mgr =m.empno;

编辑:

您选择的答案不会列出您的主席,因为它是内部联接。我想当你发现你的输出不是你的(我怀疑)家庭作业所要求的时,你就会回来。这是实际的测试用例:

> select * from emp;

 empno | ename |    job    | deptno | mgr  
-------+-------+-----------+--------+------
  7839 | king  | president |     10 |     
  7698 | blake | manager   |     30 | 7839
(2 rows)

> SELECT e.ename employee, e.empno, m.ename manager, m.empno
FROM emp AS e LEFT OUTER JOIN emp AS m
ON e.mgr =m.empno;

 employee | empno | manager | empno 
----------+-------+---------+-------
 king     |  7839 |         |      
 blake    |  7698 | king    |  7839
(2 rows)

不同之处在于外连接返回所有行。内连接将产生以下结果:

> SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM emp e, emp m
WHERE e.mgr = m.empno;

 ename | empno | manager | mgr  
-------+-------+---------+------
 blake |  7698 | king    | 7839
(1 row)
(SELECT ename FROM EMP WHERE empno = mgr)

There are no records in EMP that meet this criteria.

You need to self-join to get this relation.

SELECT e.ename AS Employee, e.empno, m.ename AS Manager, m.empno
FROM EMP AS e LEFT OUTER JOIN EMP AS m
ON e.mgr =m.empno;

EDIT:

The answer you selected will not list your president because it's an inner join. I'm thinking you'll be back when you discover your output isn't what your (I suspect) homework assignment required. Here's the actual test case:

> select * from emp;

 empno | ename |    job    | deptno | mgr  
-------+-------+-----------+--------+------
  7839 | king  | president |     10 |     
  7698 | blake | manager   |     30 | 7839
(2 rows)

> SELECT e.ename employee, e.empno, m.ename manager, m.empno
FROM emp AS e LEFT OUTER JOIN emp AS m
ON e.mgr =m.empno;

 employee | empno | manager | empno 
----------+-------+---------+-------
 king     |  7839 |         |      
 blake    |  7698 | king    |  7839
(2 rows)

The difference is that an outer join returns all the rows. An inner join will produce the following:

> SELECT e.ename, e.empno, m.ename as manager, e.mgr
FROM emp e, emp m
WHERE e.mgr = m.empno;

 ename | empno | manager | mgr  
-------+-------+---------+------
 blake |  7698 | king    | 7839
(1 row)
∞琼窗梦回ˉ 2024-12-12 07:39:58

您可以将查询更改为:

SELECT ename, empno, (SELECT ename FROM EMP WHERE empno = e.mgr)AS MANAGER, mgr 
from emp e 
order by empno;

这将告诉引擎对于内部 emp 表,empno 应与外部表中的 mgr 列匹配。
在此处输入图像描述

You could have just changed your query to:

SELECT ename, empno, (SELECT ename FROM EMP WHERE empno = e.mgr)AS MANAGER, mgr 
from emp e 
order by empno;

This would tell the engine that for the inner emp table, empno should be matched with mgr column from the outer table.
enter image description here

只怪假的太真实 2024-12-12 07:39:58

试试这个

SELECT E.ename,E.empno,ISNULL(E.ename,'NO MANAGER') AS MANAGER FROM emp e
INNER JOIN emp M
ON  M.empno=E.empno

Instaed 子查询使用自连接

TRY THIS

SELECT E.ename,E.empno,ISNULL(E.ename,'NO MANAGER') AS MANAGER FROM emp e
INNER JOIN emp M
ON  M.empno=E.empno

Instaed of subquery use self join

月下凄凉 2024-12-12 07:39:58

也许你的子查询(SELECT ename FROM EMP WHERE empno = mgr)认为,给我他们自己的经理的员工记录! (即,行的 empno 与同一行的 mgr 相同。)

您是否考虑过重写它以使用内部(自)连接? (我问,因为我什至不确定以下内容是否有效。)

SELECT t1.ename, t1.empno, t2.ename as MANAGER, t1.mgr
from emp as t1
inner join emp t2 ON t1.mgr = t2.empno
order by t1.empno;

Perhaps your subquery (SELECT ename FROM EMP WHERE empno = mgr) thinks, give me the employee records that are their own managers! (i.e., where the empno of a row is the same as the mgr of the same row.)

have you considered perhaps rewriting this to use an inner (self) join? (I'm asking, becuase i'm not even sure if the following will work or not.)

SELECT t1.ename, t1.empno, t2.ename as MANAGER, t1.mgr
from emp as t1
inner join emp t2 ON t1.mgr = t2.empno
order by t1.empno;
凡尘雨 2024-12-12 07:39:58

这是此问题的替代答案,您可以使用“分层查询”选项,而不是使用自连接。它速度很快,并且比连接有几个优点。

SELECT ename as employee, empno, prior ename as manager, prior empno as mgrno
FROM emp
start with mgr is null
connect by mgr = prior empno;

Here is the alternate answer to this question, instead of using self join you can go for the 'Hierarchical queries' option. It's fast and it has several advantages over joins.

SELECT ename as employee, empno, prior ename as manager, prior empno as mgrno
FROM emp
start with mgr is null
connect by mgr = prior empno;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文