我有一个包含empid
已分配了经理ID的表,其中empid需要用管理员名称替换管理ID。
EMPID MANAGERID EMPNAME MANAGERNAME
001 NULL ABC NULL
002 001 DEF 001
需要输出如下
EMPID MANAGERID EMPNAME MANAGERNAME
001 NULL ABC NULL
002 001 DEF ABC
答案 0 :(得分:2)
如果经理名称在同一个表中:
select t1.empID, t1.Name, t2.Name as Manager from table t1, table t2
where t1.empID=t2.ManagerID
或试试这个
select t1.EMPID, t1.MANAGERID, t1.EMPNAME, t2.EMPNAME as Manager from testTable11 t1
left join testTable11 t2 on t1.MANAGERID=t2.EMPID
答案 1 :(得分:1)
由于你没有共享Table结构,我不得不为你的问题假设它。考虑到你有以下表结构: -
CREATE TABLE employee(emp_id varchar(5) NOT NULL,
emp_name varchar(20) NULL,
dt_of_join date NULL,
emp_manager_id varchar(5) NULL,
CONSTRAINT emp_id PRIMARY KEY(emp_id) ,
CONSTRAINT emp_manager_id FOREIGN KEY(emp_manager_id) REFERENCES employee(emp_id));
在这种情况下,您可以使用以下查询列出员工的经理姓名: -
SELECT a.emp_id AS "Emp_ID",a.emp_name AS "Employee Name",
b.emp_name AS "Manager Name"
FROM employee a, employee b
WHERE a.emp_manager_id = b.emp_id;
注意:这里假设所有员工都有与其记录相关联的经理ID。
<强>更新强>
根据您的评论,请尝试以下查询,
SELECT a.emp_id AS "Emp_ID",a.emp_name AS "Employee Name",
a.emp_manager_id AS "Manager Id",
b.emp_name AS "Manager Name"
FROM employee a LEFT JOIN employee b ON a.emp_manager_id = b.emp_id;
答案 2 :(得分:0)
您需要在同一个表上进行自我(外部)联接:
select e.empid,
e.empname,
e.managerid,
mgr.empname as managername
from employee e
left join employee mgr on e.managerid = mgr.empid;