Oracle 多表查詢

2022-05-18 11:33:42 字數 3873 閱讀 2440

等值和不等值連線查詢

為了避免笛卡爾集, 可以在 where 加入有效的連線條件。

oracle 連線多表查詢

select  table1.column, table2.column

from table1, table2

where table1.column1 = table2.column2;

等值連線

select employees.employee_id, employees.last_name, 

employees.department_id, departments.department_id,

departments.location_id

from employees, departments

where employees.department_id = departments.department_id;

連線 n個表,至少需要 n-1個連線條件。 例如:連線三個表,至少需要兩個連線條件。

-- 練習:查詢出公司員工的 last_name, department_name, city

select last_name, department_name, city

from employees e ,departments d ,locations l

where e.department_id = d.department_id and d.location_id = l.location_id

非等值連線

-- employees表中的列工資應在job_grades表中的最高工資與最低工資之間

select e.last_name, e.salary, j.grade_level

from employees e, job_grades j

where e.salary between j.lowest_sal and j.highest_sal;

內連線、外連線(左(或右) 外連線、滿外連線)

內連線

select e.last_name, d.department_name 

from employees e, departments d

where e.department_id = d.department_id

-- sql1999語法的方式

select e.last_name, d.department_name

from employees e

inner join departments d on e.department_id = d.department_id

外連語法

select  table1.column, table2.column

from table1, table2

where table1.column(+) = table2.column;

-- 示例

select e.last_name, d.department_name

from employees e, departments d

where e.department_id(+) = d.department_id

-- sql1999方式

select e.last_name, d.department_name

from employees e right outer join departments d

on (e.department_id = d.department_id)

select  table1.column, table2.column

from table1, table2

where table1.column = table2.column(+);

-- 示例

select e.last_name, d.department_name

from employees e, departments d

where e.department_id = d.department_id(+)

-- sql1999方式

select e.last_name, 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

full outer join departments d

on (e.department_id = d.department_id)

自連線

-- 練習:查詢出 last_name 為 『chen』 的員工的 manager 的資訊

select worker.last_name || ' works for ' || manager.last_name

from employees worker , employees manager

where worker.manager_id = manager.employee_id and initcap(worker.last_name) = 'chen'

叉集(了解)

select last_name, department_name

from employees

cross join departments

自然連線

返回的是,兩個表中具有相同名字的列的「且、交集」,而非「或,並集」。即:比如employee類和department類都有

department_id和manager_id,返回二者都相同的結果。

select department_id, department_name,

location_id, city

from departments

natural join locations

使用using建立連線

select  last_name,department_name 

from employees

join departments using (department_id);

使用on 子句建立連線 (常用)

select 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)

-- 使用on 建立多表連線

select employee_id, city, 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

Oracle 多表查詢

sql 外連線 sql 按部門統計員工人數 部門號 部門名稱 人數 sql select d.deptno,d.dname,count e.empno 2 from dept d,emp e 3 where d.deptno e.deptno 4 group by d.deptno,d.dname ...

Oracle 多表查詢

等值和不等值連線查詢 為了避免笛卡爾集,可以在 where 加入有效的連線條件。oracle 連線多表查詢 在 where 子句中寫入連線條件。在表中有相同列時,在列名之前加上表名字首 select table1.column,table2.column from table1,table2 whe...

oracle 多表查詢

多表查詢 多表查詢,又稱表聯合查詢,即一條sql語句涉及到的表有多張,資料通過特定的連線進行聯合顯示.笛卡爾積 在數學中,兩個集合x和y的笛卡尓積 cartesian product 又稱直積,表示為x y.假設集合a 集合b 則兩個集合的笛卡爾積為。在資料庫中,如果直接查詢倆張表,那麼其查詢結果就...