5

以下是我的 CREATE TABLE 脚本:

create table EMPLOYEES
    (EmpID    char(4)         unique Not null,
     Ename    varchar(10),
     Job      varchar(9),
     MGR      char(4),
     Hiredate date,
     Salary   decimal(7,2),
     Comm     decimal(7,2),
     DeptNo   char(2)         not null,
         Primary key(EmpID),
         Foreign key(DeptNo) REFERENCES DEPARTMENTS(DeptNo));

以下是我的 INSERT 脚本:

insert into EMPLOYEES values (7839,'King','President',null,'17-Nov-11',5000,null,10);
insert into EMPLOYEES values (7698,'Blake','Manager',7839,'01-May-11',2850,null,30);
insert into EMPLOYEES values (7782,'Clark','Manager',7839,'02-Jun-11',2450,null,10);
insert into EMPLOYEES values (7566,'Jones','Manager',7839,'02-Apr-11',2975,null,20);
insert into EMPLOYEES values (7654,'Martin','Salesman',7698,'28-Feb-12',1250,1400,30);
insert into EMPLOYEES values (7499,'Allen','Salesman',7698,'20-Feb-11',1600,300,30);
insert into EMPLOYEES values (7844,'Turner','Salesman',7698,'08-Sep-11',1500,0,30);
insert into EMPLOYEES values (7900,'James','Clerk',7698,'22-Feb-12',950,null,30);
insert into EMPLOYEES values (7521,'Ward','Salesman',7698,'22-Feb-12',1250,500,30);
insert into EMPLOYEES values (7902,'Ford','Analyst',7566,'03-Dec-11',3000,null,20);
insert into EMPLOYEES values (7369,'Smith','Clerk',7902,'17-Dec-10',800,null,20);
insert into EMPLOYEES values (7788,'Scott','Analyst',7566,'09-Dec-12',3000,null,20);
insert into EMPLOYEES values (7876,'Adams','Clerk',7788,'12-Jan-10',1100,null,20);
insert into EMPLOYEES values (7934,'Miller','Clerk',7782,'23-Jan-12',1300,null,10);

以下是我的 SELECT 脚本:

select distinct e.Ename as Employee, m.mgr as reports_to
from EMPLOYEES e
inner join Employees m on e.mgr = m.mgr;

我用相应的经理 ID 获取员工;

Ford    7566
Scott   7566
Allen   7698
James   7698
Martin  7698
Turner  7698
Ward    7698
Miller  7782
Adams   7788
Blake   7839
Clark   7839
Jones   7839
Smith   7902

我如何同时列出经理姓名? *我在做正确的内部连接吗?*

4

10 回答 10

14

添加m.Ename到您的SELECT查询:

select distinct e.Ename as Employee, m.mgr as reports_to, m.Ename as Manager
from EMPLOYEES e
inner join Employees m on e.mgr = m.EmpID;
于 2013-03-28T09:50:30.890 回答
2

您的查询已关闭,您需要使用mgr和加入empid

on e1.mgr = e2.empid

所以完整的查询是:

select e1.ename Emp,
  e2.eName Mgr
from employees e1
inner join employees e2
  on e1.mgr = e2.empid

请参阅带有演示的 SQL Fiddle

如果您想返回所有行,包括那些没有经理的行,那么您可以将其更改为LEFT JOIN(例如总统):

select e1.ename Emp,
  e2.eName Mgr
from employees e1
left join employees e2
  on e1.mgr = e2.empid

请参阅带有演示的 SQL Fiddle

样本数据中的总裁将为null经理返回一个值,因为他们没有经理。

于 2013-03-28T09:52:11.100 回答
1

不,正确的连接是:

inner join Employees m on e.mgr = m.EmpID;

您需要将当前员工的 ManagerID 与经理的 EmployeeID 匹配。不使用经理的 ManagerID。

更新
正如 Andrey Gordeev 所指出的:
您还需要添加m.Ename到您的SELECT查询中,以便在您的结果中获取 Manager 的名称。否则你只会得到managerID。

于 2013-03-28T09:49:06.923 回答
1

ON在连接处有一个不正确的子句,这有效:

inner join Employees m on e.mgr = m.EmpId;

mgr 列引用该EmpId列。

演示

于 2013-03-28T09:50:46.077 回答
1
select a.empno,a.ename,a.job,a.mgr,B.empno,B.ename as MGR_name, B.job as MGR_JOB from 
    emp a, emp B where a.mgr=B.empno ;
于 2016-08-27T21:50:13.223 回答
0
SELECT DISTINCT e.Ename AS Employee, 
    m.mgr AS reports_to, 
    m.Ename AS manager 
FROM Employees e, Employees m 
WHERE e.mgr=m.EmpID;
于 2013-03-28T09:55:27.150 回答
0
select e.ename as Employee, m.ename as Manager
from emp e, emp m
where e.mgr = m.empno

如果您想获得所有记录的结果(无论它们是否向任何人报告),请在第二个表的名称上附加 (+)

select e.ename as Employee, m.ename as Manager
from emp e, emp m
where e.mgr = m.empno(+)
于 2015-02-16T13:06:52.943 回答
0
Select e.lastname as employee ,m.lastname as manager
  from employees e,employees m
 where e.managerid=m.employyid(+)
于 2015-08-26T08:08:22.417 回答
0

共有三个表 - Equities(coulmns: ID,ISIN) 和 Bond(coulmns: ID,ISIN)。第三个表 Securities(columns: ID,ISIN) 包含来自 Equities 和 Bond 表的所有数据。编写 SQL 查询以验证以下内容: (1) Securities 表应包含 Equities 和 Bonds 表中的所有数据。(2) Securities 表不应包含除 Equities 和 Bonds 表之外的任何数据

于 2017-10-13T10:19:06.320 回答
-1

问题:-.显示员工姓名、他的加入日期、他的经理姓名和他的经理的加入日期。ANS:- 选择 e1.ename Emp,e1.hiredate, e2.eName Mgr,e2.hiredate from emp e1, emp e2 where e1.mgr = e2.empno

于 2018-05-18T14:05:23.217 回答