2014-04-10 64 views
1

我一直在試圖弄清楚這一段時間,仍然沒有運氣。我是否將以下內容與'is null'結合?如何在單個SQL查詢中顯示:哪些部門沒有任何員工,哪些員工沒有部門(Oracle SQL)?

select distinct 
     e.employee_id, e.last_name, e.department_id, 
     d.department_id, d.location_id 
from employees e 
    join departments d on (e.department_id = d.department_id) 
+2

谷歌對於Oracle FULL OUTER JOIN。基本上你需要做一個完整的外連接,並選擇left或right爲null的地方。在Oracle中執行此操作的一種方法是使用UNION,但Google有更多信息。 – Joe

回答

2

例如基於Joe的評論

select distinct 
     e.employee_id, e.last_name, e.department_id, 
     d.department_id, d.location_id 
from employees e 
    full outer join departments d on (e.department_id = d.department_id) 
where e.department_id is null or d.department_id is null 

或者這種方式與union

select distinct 
     e.employee_id, e.last_name, e.department_id, 
     d.department_id, d.location_id 
from employees e 
    left outer join departments d on (e.department_id = d.department_id) 
where d.department_id is null 
union 
select distinct 
     e.employee_id, e.last_name, e.department_id, 
     d.department_id, d.location_id 
from employees e 
    right outer join departments d on (e.department_id = d.department_id) 
where e.department_id is null 
相關問題