0

我编写了这个查询来显示在多伦多工作的所有员工的姓氏、部门编号和部门名称。

select last_name, job_id, department_id, department_name
from employees e
 join departments d on d.department_id=e.department_id
 join locations l on d.location_id=l.location_id and l.city='Toronto';

我收到此错误 ORA-00918: column ambiguously defined

4

4 回答 4

2

当参与连接的两个表上都存在列时,您需要在列名前加上别名,以指定您想要的列。在您的联接中department_id,两个表共享,您可以指定要d.department_id在选定列列表中使用的列。

select 
  last_name, 
  job_id, 
  d.department_id, --specify which table you want this ambiguous column from
  department_name
from employees e
  join departments d 
  on d.department_id=e.department_id
  join locations l on 
  d.location_id=l.location_id and l.city='Toronto';
于 2013-05-04T09:50:22.333 回答
2

使用别名从任何特定表中选择列。例如,简单地写department_id会引发错误,因为它在多个表上可用并且会引发歧义错误

因此,更好的解决方案是选择具有别名的列,例如

 select e.last_name, e.job_id, e.department_id, d.department_name
 from employees e
 join departments d on d.department_id=e.department_id
 join locations l on d.location_id=l.location_id and l.city='Toronto';
于 2013-05-04T09:50:42.687 回答
1

将第一行更改为:

select e.last_name, e.job_id, e.department_id, d.department_name
于 2013-05-04T09:49:24.313 回答
0

尝试这个:

select last_name, job_id, department_id, department_name 
from employees 
join departments using(department_id) 
join locations using(location_id) where city='Toronto';
于 2017-08-08T18:48:24.300 回答