尝试在单个表中显示员工主管的名称

时间:2017-11-03 03:13:22

标签: mysql database

这是我的数据库

Drop Table Staff;

Create Table Staff
(StaffNo  varchar(6) Primary Key,
Lastname  varchar(30),
Firstname varchar(25),
Hire_date  date,
Location  varchar(30),
SupervisorNo  varchar(6),
Salary  numeric(8,2),
Commission  numeric(4,2));

-- Populate Staff Table ------------------------------------------

Insert Into Staff
Values('000001','Zambini','Rick',Date('1980-2-15'),'LOS 
ANGELES','000000',6000.00,5.00);
Insert Into Staff
Values('000003','Vidoni','Cheryl',Date('1980-3-6'),'NEW 
YORK','000000',5780.00,5.00);
Insert Into Staff
Values('000004','Coudray','Sandy',Date('1980-6-6'),'LOS 
ANGELES','000001',6237.00,5.00);
Insert Into Staff
Values('000006','Thomas','Pat',Date('1991-1-8'),'NEW 
YORK','000003',5875.00,5.00);
Insert Into Staff
Values('000008','McLester','Debbie',Date('1981-4-12'),'LOS 
ANGELES','000001',4792.00,5.00);
Insert Into Staff
Values('000011','Michaels','Delores',Date('1982-5-
5'),'CHICAGO','000012',4927.00,7.00);
Insert Into Staff
Values('000012','Charles','Ted',Date('1983-2-
2'),'CHICAGO','000000',5945.00,5.00);
Insert Into Staff
Values('000013','Marin','Mark',Date('1983-6-5'),'LOS 
ANGELES','000001',4802.00,11.00);
Insert Into Staff
Values('000015','Roddick','Mary',Date('1984-2-13'),'NEW 
YORK','000003',5493.00,8.00);
Insert Into Staff
Values('000016','Long','Nicole',Date('1984-8-18'),'NEW 
YORK','000003',5190.00,7.00);
Insert Into Staff
Values('000019','Rolfes','Chuck',Date('1984-9-9'),'LOS 
ANGELES','000001',4586.00,6.00);
Insert Into Staff
Values('000020','Sanders','Kathy',Date('1985-3-
23'),'CHICAGO','000012',3783.00,5.00);

COMMIT;

我必须显示McLester的主管名称,但我找不到将supervisorNo与姓氏相关联的方法

这一个让我真的很难过

我无法进行连接,因为这是一个表,我无法找到一种方法将supervisorno链接到lastname值,而无需在数据中查找答案

select *
from staff
where  supervisorno in (select supervisorno from staff) and lastname = 
'mclester';  

到目前为止我的查询

2 个答案:

答案 0 :(得分:0)

SELECT * FROM staff where staffno = (SELECT supervisorno FROM staff WHERE lastname = 'mclester');之类的内容有效吗?

我知道它不漂亮,但我认为它应该有用。

答案 1 :(得分:0)

select * from staff where StaffNo  IN (select SupervisorNo from staff where Lastname = 'McLester');