我是SQLite的新手。我想显示每个员工的姓名和他/她经理的姓名。但我对如何将managerID与EmployeeID链接有点困惑?我需要使用子查询吗?
table: Employee
Columns: EmployeeID [pk], EmployeeName, ManagerID
像:
EmployeeID EmployeeName ManagerID
1 Alice 2
2 Peter null
3 John 2
4 Mary 1
意味着Peter是Alice和John的经理,而Alice是Mary的经理。
SELECT EmployeeName AS Employee, EmployeeName AS Manager
FROM Employee
WHERE
答案 0 :(得分:1)
您需要使用LEFT OUTER join加入表格。由于老板没有经理(null),请使用“ifnull”:
select A.EmployeeName as 'EmployeeName',
ifnull(B.EmployeeName, 'BOSS') as 'ManagerName'
from employee A left outer join employee B
on A.managerId = B.employeeId;