以下是我的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
如何列出经理姓名? *我正在做正确的内部联接吗?*
答案 0 :(得分:13)
将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;
答案 1 :(得分: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
如果你想要返回所有行,包括那些没有经理的行,那么你可以将它改为LEFT JOIN
(例如总统):
select e1.ename Emp,
e2.eName Mgr
from employees e1
left join employees e2
on e1.mgr = e2.empid
示例数据中的总裁将为经理返回null
值,因为他们没有经理。
答案 2 :(得分:1)
不,正确的连接是:
inner join Employees m on e.mgr = m.EmpID;
您需要将当前员工的ManagerID与经理的EmployeeID相匹配。不是经理的经理ID。
<强>更新强>
正如Andrey Gordeev所说:
您还需要在m.Ename
查询中添加SELECT
,以便在结果中获取经理的名称。否则你只会得到经理ID。
答案 3 :(得分:1)
答案 4 :(得分: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 ;
答案 5 :(得分: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;
答案 6 :(得分: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(+)
答案 7 :(得分:0)
有三个表 - 股票(coulmns:ID,ISIN)和Bond(coulmns:ID,ISIN)。第三表证券(coulmns:ID,ISIN)包含来自股票和债券表的所有数据。 编写SQL查询以在下面进行验证: (1)证券表应包含股票和债券表中的所有数据。 (2)证券表不应包含股票和债券表中除以外的任何数据
答案 8 :(得分:-1)
问题: - 。显示员工姓名,他的加入日期,他的经理姓名&amp;他的经理人的加入日期。 ANS: - 选择e1.ename Emp,e1.hiredate, e2.eName Mgr,e2.hiredate 来自emp e1,emp e2 其中e1.mgr = e2.empno