如何在SQL查询中显示employee表中的经理姓名?
我有一个employee表,显示员工编号、姓名和经理的员工编号。我正在尝试创建一个显示emp名称、emp、经理名称和经理的查询 该表如下所示:如何在SQL查询中显示employee表中的经理姓名?,sql,oracle,Sql,Oracle,我有一个employee表,显示员工编号、姓名和经理的员工编号。我正在尝试创建一个显示emp名称、emp、经理名称和经理的查询 该表如下所示: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7839 KING PRESIDENT - 17-NOV-81 5000 - 10 7698 BLAKE MANAGER 7839 01-MAY-81 2850 - 30 7782 CLARK MANAGER
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7839 KING PRESIDENT - 17-NOV-81 5000 - 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 - 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 - 10
7566 JONES MANAGER 7839 02-APR-81 2975 - 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 - 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7902 FORD ANALYST 7566 03-DEC-81 3000 - 20
其中三列非常简单,因为它们在表中,但是当我只有经理的员工编号时,如何添加经理姓名
这是我到目前为止只显示了3列的内容
select ename as "Employee", empno as "Emp#", mgr as "Mgr#" from emp;
任何帮助都将不胜感激请尝试以下方法:
select
emp.ename as "Employee",
emp.empno as "Emp#",
emp.mgr as "Mgr#",
m.ename as "Manager"
from
emp
LEFT OUTER JOIN emp m ON
emp.mgr = m.empno
select e.ename ,e.sal, (select s.grade from salgrade s where e.sal between s.losal and s.hisal) empgrade ,e2.ename"mgr", e2.sal mgrsal,
(select s.grade from salgrade s where e2.sal between s.losal and s.hisal) mgrgrade from emp e,emp e2
where e.mgr=e2.empno(+)
试试这个:
select e.ename ,e.sal, (select s.grade from salgrade s where e.sal between s.losal and s.hisal) empgrade ,e2.ename"mgr", e2.sal mgrsal,
(select s.grade from salgrade s where e2.sal between s.losal and s.hisal) mgrgrade from emp e,emp e2
where e.mgr=e2.empno(+)
这将为您提供不同的员工姓名,他们是经理
或:
或:
这将为您提供不同的员工姓名,他们是经理
或:
或:
我试过了,效果很好
SELECT a.empno EmpID,a.empname EmpName, a.mgrid MgrID,(select empname from employee where empno = a.mgrid) MgrName
from employee a, employee b
where a.empno = b.empno order by a.empno asc;
我试过了,效果很好
SELECT a.empno EmpID,a.empname EmpName, a.mgrid MgrID,(select empname from employee where empno = a.mgrid) MgrName
from employee a, employee b
where a.empno = b.empno order by a.empno asc;
请尝试此查询
SELECT E2.EMPID,E2.EMPNAME,E1.EMPNAME
AS MGRNAME
FROM employee E1
INNER JOIN employee E2 ON E1.Empid=E2.ManagerId
请尝试此查询
SELECT E2.EMPID,E2.EMPNAME,E1.EMPNAME
AS MGRNAME
FROM employee E1
INNER JOIN employee E2 ON E1.Empid=E2.ManagerId
通常,对于计算工作,我们只需要数据——不修饰。UI和装饰应该留给一个单独的层,而不应该处理或装饰空值!在做所有这些事情的同时,你的答案也漏掉了所有其他(有用的)专栏。通常对于计算工作,我们只需要数据——不修饰。UI和装饰应该留给一个单独的层,而不应该处理或装饰空值!在做所有这些事情的同时,你的答案也漏掉了所有其他(有用的)专栏。
select e.ename as "Employee", e.empno as "Emp#", e.mgr as "Mgr#", m.ename as "MgrName" from emp e , emp m on e.mgr = m.empno
SELECT a.empno EmpID,a.empname EmpName, a.mgrid MgrID,(select empname from employee where empno = a.mgrid) MgrName
from employee a, employee b
where a.empno = b.empno order by a.empno asc;
SELECT E2.EMPID,E2.EMPNAME,E1.EMPNAME
AS MGRNAME
FROM employee E1
INNER JOIN employee E2 ON E1.Empid=E2.ManagerId
Select e.ename empname , m.ename managername,e.empno empno ,m.mgr managerno from emp e, mgr m
where m.empno=e.mgr