多表連接
select table1.aaa,table2.bbb from table1
–cross join table2
–natural join table2
–join table2 using (xxx)
–join table on table1.table1_id = table2.table2 _id
–left/right/full (outer) join table2 on table1.table1_id = table2.table2 _id
內連接(等值連接)
select e.last_name,d.department_name from employees e,departments d where e.department_id=d.department_id = select e.last_name,d.department_name from employees e join departments d on e.department_id=d.department_id
外連接 left/right join 表名 on
left join 表名 on select e.last_name,d.department_name from employees e left join departments d on e.department_id=d.department_id = 使用(+), 帶(+)的爲補充表 select e.last_name,d.department_name from employees e,departments d where e.department_id=d.department_id(+)
自連接
select worker.last_name ||' works for '|| manager.last_name from employees worker,employees manager where worker.manager_id = manager.employee_id
自然連接 natural 表名 join
以兩個表中名字相同的列爲條件創建等值連接
如果有相同名字的列數據類型不同,返回一個錯誤
select * from employees natural join departments = select * from employees e,departments d where e.department_id=d.department_id and d.manager_id=e.manager_id using指定等值連接用到的列 join 表名 using , natural join 與 using 是互相排斥的 在引用列不要使用表名或別名 select * from employees join departments using (department_id) = select * from employees e,departments d where e.department_id=d.department_id select * from employees join departments using (department_id) where department_id = 10 select * from employees join departments d using (department_id) where d.department_id = 10 --錯誤的
交叉連接 cross 表名 join
兩個表的交叉乘積,該連接和兩個表之間的笛卡爾乘積是一樣的
select last_name,department_name from employees cross join departments = select last_name,department_name from employees,departments
全外連接 full join 表名 on
返回兩個表中的行,即使不匹配的行也會返回
select * from employees e full join departments d on e.department_id=d.department_id