我需要找到其主管的主管SSN为'888665555'的所有员工
我似乎无法弄清楚我做错了什么。
以下是正在使用的表的副本。
Fname Lname Ssn Super_ssn
john smith 123456789 333445555
franklin wong 333445555 888665555
alicia zelaya 999887777 987654321
jennifer wallace 987654321 888665555
ramesh narayan 666884444 333446666
joyce english 453453453 333445555
ahmad jabbar 987987987 987654321
james borg 888665555 NULL
我一直在尝试的SQL代码如下。
SELECT EMPLOYEE.Fname, EMPLOYEE.Lname
FROM EMPLOYEE
WHERE EMPLOYEE.Super_ssn =
(SELECT EMPLOYEE.Ssn
FROM EMPLOYEE
WHERE EMPLOYEE.Super_ssn = '888665555');
结果应如下所示:
Fname Lname
john smith
alicia zelaya
ramesh narayan
joyce english
ahmad jabbar
答案 0 :(得分:4)
感谢NSNoob,我找到了答案。
SELECT EMPLOYEE.Fname, EMPLOYEE.Lname
FROM EMPLOYEE
WHERE EMPLOYEE.Super_ssn IN
(SELECT EMPLOYEE.Ssn
FROM EMPLOYEE
WHERE EMPLOYEE.Super_ssn = '888665555');
这会获取每个结果,而不是仅接受一个结果。
答案 1 :(得分:3)
下面的子查询返回的结果不止一个。
(SELECT EMPLOYEE.Ssn
FROM EMPLOYEE
WHERE EMPLOYEE.Super_ssn = '888665555');
像
一样修改它 (SELECT top 1 EMPLOYEE.Ssn
FROM EMPLOYEE
WHERE EMPLOYEE.Super_ssn = '888665555');
或类似的东西,它只通过子查询返回一个结果。
答案 2 :(得分:1)
MS Access sql没有递归选择。但是,如果您知道深度,则可以执行标准(双)连接。问题是你的主管只有一名超级监督员吗?
如果您只有两个级别:尝试此查询:
SELECT
tbl_employee.fname,
tbl_employee.lname,
tbl_employee_supervisor.ssn,
tbl_employee_super_supervisor.ssn
FROM
(tbl_employee
LEFT JOIN tbl_employee AS tbl_employee_supervisor ON tbl_employee.super_ssn = tbl_employee_supervisor.ssn)
LEFT JOIN
tbl_employee AS tbl_employee_super_supervisor ON tbl_employee_supervisor.super_ssn = tbl_employee_super_supervisor.ssn
WHERE
(((tbl_employee_supervisor.ssn) = '888665555'))
OR (((tbl_employee_super_supervisor.ssn) = '888665555'));
结果:
fname lname supervisor.ssn super_supervisor.ssn
john smith 333445555 888665555
franklin wong 888665555
alicia zelaya 987654321 888665555
jennifer wallace 888665555
joyce english 333445555 888665555
ahmad jabbar 987654321 888665555