26. 多表連線查詢時, 若兩個表有同名的列, 必須使用表的別名對列名進行引用, 否則出錯!
27. 查詢出公司員工的 last_name, department_name, city
select last_name, department_name, city
from departments d, employees e, locations l
where d.department_id = e.department_id and d.location_id = l.location_id
28. 查詢出 last_name 為 'chen' 的 manager 的資訊. (員工的 manager_id 是某員工的 employee_id)
0). 例如: 老張的員工號為: "1001", 我的員工號為: "1002",
我的 manager_id 為 "1001" --- 我的 manager 是"老張"
1). 通過兩條 sql 查詢:
select manager_id
from employees
where lower(last_name) = 'chen' --返回的結果為 108
select *
from employees
where employee_id = 108
2). 通過一條 sql 查詢(自連線):
select m.*
from employees e, employees m
where e.manager_id = m.employee_id and e.last_name = 'chen'
3). 通過一條 sql 查詢(子查詢):
select *
from employees
where employee_id = (
select manager_id
from employees
where last_name = 'chen'
) 29. 查詢每個員工的 last_name 和 grade_level(在 job_grades 表中). ---- 非等值連線
select last_name, salary, grade_level, lowest_sal, highest_sal
from employees e, job_grades j
where e.salary >= j.lowest_sal and e.salary <= j.highest_sal
30. 左外連線和右外連線
select last_name, e.department_id, department_name
from employees e, departments d
where e.department_id = d.department_id(+)
select last_name, d.department_id, department_name
from employees e, departments d
where e.department_id(+) = d.department_id
理解 "(+)" 的位置: 以左外連線為例, 因為左表需要返回更多的記錄,
注意: 1). 兩邊都加上 "(+)" 符號, 會發生語法錯誤!
2). 這種語法為 oracle 所獨有, 不能在其它資料庫中使用.
31. sql 99 連線 employees 表和 departments 表
1).select *
from employees join departments
using(department_id)
缺點: 要求兩個表中必須有一樣的列名.
2).select *
from employees e join departments d
on e.department_id = d.department_id
3).多表連線
select e.last_name, d.department_name, l.city
from employees e join departments d
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
32. sql 99 的左外連線, 右外連線, 滿外連線
1).select last_name, department_name
from employees e left outer join departments d
on e.department_id = d.department_id
2).select last_name, department_name
from employees e right join departments d
on e.department_id = d.department_id
3).select last_name, department_name
from employees e full join departments d
on e.department_id = d.department_id
1. 顯示所有員工的姓名,部門號和部門名稱。
a) select last_name,e.department_id,department_name
b) from employees e,departments d
c) where e.department_id = d.department_id(+)
方法二:
select last_name,e.department_id,department_name
from employees e left outer join departments d
on e.department_id = d.department_id
2. 查詢90號部門員工的job_id和90號部門的location_id
a) select distinct job_id,location_id
b) from employees e left outer join departments d
c) on e.department_id = d.department_id
d) where d.department_id = 90
3. 選擇所有有獎金的員工的
last_name , department_name , location_id , city
select last_name,department_name,d.location_id,city
from employees e join departments d
on e.department_id = d.department_id
join locations l
on d.location_id = l.location_id
where e.commission_pct is not null
4. 選擇city在toronto工作的員工的
last_name , job_id , department_id , department_name
select last_name , job_id , e.department_id , department_name
from employees e ,departments d,locations l
where e.department_id = d.department_id and l.city = 'toronto' and d.location_id = l.location_id
5. 選擇指定員工的姓名,員工號,以及他的管理者的姓名和員工號,結果類似於下面的格式
employees emp# manager mgr#
kochhar 101 king 100
select e1.last_name "employees",e1.employee_id "emp#",e2.last_name"manger",e2.employee_id "mgr#"
from employees e1,employees e2
where e1.manager_id = e2.employee_id(+)
oracle之單行函式
單行函式 字元函式 lower x 將x中的每個單詞都轉換成小寫 upper x 將x中的每個單詞都轉換成大寫 initcap x 將x中的每個單詞的首字母轉換成大寫 concat x,y 用於將y新增到x之後,與 功能一樣 substr x,start length 用於從x中取得從start位置...
Oracle之多表查詢
多表查詢的基礎語法 select 查詢列 1 別名1,查詢列 2 別名2,from 表名稱1 別名1 名稱2 別名2 給出幾個例子吧。1.select e.ename,e.sal,d.dname,s.grade,m.ename,m.sal,ms.grade from emp e,dept d,sal...
oracle之多表連線
資料庫中的資料不可能都放在同一張表中,這樣效率是很低下的,所以都是分表儲存。但是我們往往需要獲取的資料並不會都集中在一張表中,而是多張表都會涉及到,那怎麼從多張表中獲取到我們的資料?這個時候就會用到多表連線 在多個表之間通過一定的連線條件,使表之間 發生關聯,進而能從多個表之間獲取資料。連線就是從笛...