一般的相等連接:
select * from a, b where a.id = b.id;
這個就屬於內連接。
對於外連接:
Oracle中可以使用“(+) ”來表示,9i可以使用LEFT/RIGHT/FULL OUTER JOIN
LEFT OUTER JOIN:左外關聯
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id);
等價於
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id=d.department_id(+)
結果為:所有員工及對應部門的記錄,包括沒有對應部門編號department_id的員工記錄。
RIGHT OUTER JOIN:右外關聯
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id);
等價於
SELECT e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id(+)=d.department_id
結果為:所有員工及對應部門的記錄,包括沒有任何員工的部門記錄。
FULL OUTER JOIN:全外關聯
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON (e.department_id = d.department_id);
結果為:所有員工及對應部門的記錄,包括沒有對應部門編號department_id的員工記錄和沒有任何員工的部門記錄。
ORACLE8i是不直接支持完全外連接的語法,也就是說不能在左右兩個表上同時加上(+),下面是在Oracle8i可以參考的完全外連接語法
select t1.id,t2.id from table1 t1,table t2 where t1.id=t2.id(+)
union
select t1.id,t2.id from table1 t1,table t2 where t1.id(+)=t2.id