有两个表
我需要显示员工ID,员工名称,经理姓名和经理ID,但我不知道如何显示经理姓名。这是我所拥有的,即使它是非常错误的并且没有显示真实的经理姓名
SELECT e.last_name "Employee", e.employee_id "Emp#", e.last_name "Manager",
d.manager_id "Mgr#"
FROM employees e,
departments d;
答案 0 :(得分:1)
使用关键字Join弄清楚了-仅需要两次使用一张表
SELECT e.last_name "Employee", e.employee_id "Emp#", e2.last_name "Manager",
e.manager_id "Mgr#"
FROM employees e
JOIN employees e2 ON e2.employee_id = e.manager_id;
或者没有关键字加入/使用简单加入
SELECT e.last_name "Employee", e.employee_id "Emp#", e2.last_name "Manager",
e.manager_id "Mgr#"
FROM employees e, employees e2
WHERE e2.employee_id = e.manager_id
答案 1 :(得分:0)
您可以在manager_id上进行内部联接,以便仅保留匹配的行
SELECT last_name as "Employee", employee_id as "Emp#", last_name as "Manager",
manager_id as "Mgr#"
FROM employees as e1
INNER JOIN departments as d2
ON e1.manager_id = d2.manager_id;