2013-05-04 69 views
0

我已經寫了這個查詢來顯示在多倫多工作的所有員工的姓氏,部門號碼和部門名稱。在oracle 10g中加入查詢

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:列定義的含糊

回答

1

變化的第一行:

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

其工作yahia。謝謝。因此,我的理解。所以如果我們使用多個表來檢索,總是需要使用表別名? – Koneri 2013-05-04 09:55:39

+0

@ user2186265不一定必要,但如果這些表中的任何列名存在多次,則需要使用別名... – Yahia 2013-05-04 09:56:59

+0

如果涉及多個表時使用表別名,這將是一種很好的做法。對於未來的員工來說,它更強大,更容易處理代碼。 – 2013-05-04 10:10:44

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'; 
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'; 
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';