例如:按照department_id查詢employee(員工表)和department(部門表)的資訊。
方法一:(通用型):select …from…where
select e.last_name,e.department_id,d.department_name
from employee e,department d
where e.department_id=d.department_id
方法二:select …from …natural join…
有侷限性:會自動連線兩個表中相同的列(可能有多個:department_id和manager_id)
select last_name,department_id,department_name
from employees
natural
join departments;
方法三:select …join…using…
有侷限性:好於方法二,但若多表的連線列列名不同,此法不合適
select last_name,department_id,department_name
from employees
join departments
using
(department_id)
;
方法四:select …from …join…on…
常用方法:較方法一,更易實現外聯接(左、右、滿)
select last_name,e.department_id,department_name
from employees e
join departments d
on e.department_id = d.department_id;
–內連線
--等值連線
--不等值連線
--非自然連線
--自連線
–外連線
--左外連線、右外連線、滿外連線
習題:
多表連線查詢時,若兩個表有相同的列,必須使用表的別名對列名進行引用,否則出錯!
查詢出公司員工的last_name,department_name,city
select last_name,department_name,city
from department d,employees e,location l
where d.department_id = e.department_id and d.location_id = l.location_id;
查詢出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 m.
*from employees e, employees m
where e.manager_id =
108;
2).通過一條sql查詢(自連線):
select m.
*from employees e,employees m
where e.manager_id = m.employee_id and e.last_name=
'chen'
;
.通過一條sql查詢(子查詢):
select
*from employees
where employee_id=
(select manager_id
from employees
where last_name=
'chen'
);
查詢每個員工的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;
左外連線和右外連線
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所獨有,不能在其他資料庫中使用。
sql 99 的左外連線,右外連線,滿外連線
--1).
select last_name.department_name
from employee 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;
sql 99連線employees表和department表
--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 location l
on d.location_id = l.location_id;
第四章高階查詢
案例1 檢查 oop 課程最近一次考試。如果有80分以上的成績,則每人提2分 否則,每人提5分。最終的成績不得大於100分 if exists select studentresult from result where subjectid select subjectid from subject...
第四章 sql 動態查詢
declare va number 10 vb number 5 begin execute immediate create table temp1 a number 10 b number 5 execute immediate select a b from temp1 into va,vb ...
第四章 繼承
一 為什麼要繼承 在物件導向中我們將具有很多重複內容的類中的內容提取出來,寫成乙個單獨的類 其他類只需要繼承就能取得這些功能,同時可以在自己類中寫入獨特的自定義方法 二 繼承語法 inte ce circle nsobject 繼承是在介面中定義的 冒號後的類名是要整合的類,nsobject 是co...