在oracle 10g中加入查询

时间:2013-05-04 09:44:02

标签: sql oracle oracle10g

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

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:列模糊定义

4 个答案:

答案 0 :(得分: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';

答案 1 :(得分: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';

答案 2 :(得分:1)

将第一行更改为:

select e.last_name, e.job_id, e.department_id, d.department_name

答案 3 :(得分: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';
相关问题