SQL自连接&哪里

时间:2016-01-26 17:00:00

标签: sql

我需要一个正确的SQL查询解决方案,我被要求显示部门ID在60到80之间的经理及其员工的姓名。

select department_id, a.first_name as worker,b.first_name as manager
from employees a 
join employees b
  on a.manager_id = b.employee_id
where department_id > 60 
  and department_id < 80

2 个答案:

答案 0 :(得分:1)

如果它无法正常工作,您能否显示产生的错误?

到目前为止我发现的是你应该设置一个。和b。对于department_id,否则列是不明确的:不清楚来自员工a或员工b的department_id。

select a.department_id, a.first_name as worker,b.first_name as manager
from employee a join employee b on a.manager_id=b.employee_id where 
a.department_id>60 and a.department_id<80 and b.department_id>60 and 
b.department_id<80 

有关详细信息,请参阅http://www.tutorialspoint.com/sql/sql-self-joins.htm

答案 1 :(得分:0)

使用此

 select a.department_id, a.first_name as worker,b.first_name as manager
 from employees a join employees b
 on a.manager_id=b.employee_id
 where a.department_id>60 and a.department_id<80

在where子句中a.department_id位于department_id,而选择则有相同的更改。否则会有冲突的列名。我们必须告诉sql哪个表,a或b用于select和where子句。