如果给你以下结构给出的表格。
empid | empname | salary | managerid
如何找到结果
empid | empname | salary | managername
因为经理也是员工的一部分
答案 0 :(得分:1)
SELECT
x.empid,
x.empname as 'EmployeeName',
x.salary as 'Salary',
(SELECT empname FROM Tablename WHERE empid = x.managerid) as 'ManagerName'
FROM
Tablename x
答案 1 :(得分:0)
您可以进行自我加入,将第一个表(员工)的ManagerId
链接到第二个使用员工(经理)的EmpId
:
SELECT e.empid, e.empname, e.salary
, COALESCE(m.empname, 'NoManager') AS ManagerName
FROM Employee AS e
LEFT JOIN Employee AS m ON e.ManagerId = m.empId