Oracle總結筆記(二)

10 . 僞列:虛列,在原表中不存在的列,可通過select語句查詢到。
I. 語法:select 僞列 from 表名
II. 詳解:
rowid【瞭解】:唯一標識一行數據的物理位置。 //Oracle查詢優化器,內部查詢時使用比較多,應用開發很少用
rownum【重點】:爲查詢出的每一行符合要求的數據分配邏輯編號。 //從1開始,依次遞增1。
III. 用例:

//基於rowid進行一行數據的查詢(速度快)
select * from employees where rowid = 'AAAR5kAAFAAAADNABD'  file# block# row# 

//查詢所有列和rowid(表別名 e ,不允許使用as)
select e.*, rowid from employees e

//查詢員工表中前5名員工信息(編號,名字,薪資)
select employee_id , first_name , salary
from employees
where rownum <= 5;

//查詢員工表中第6名之後員工信息(編號,名字,薪資)
select employee_id , first_name , salary , rownum
from employees
where rownum > 5; //error沒有符合條件 的結果
注:rownum總是從1開始生成,只在 <= 的環境下使用  在>=的環境下使用無效

//查詢員工表中工資排名前5員工的信息(編號,名字,薪資)
select employee_id , first_name , salary
from employees
where rownum <= 5
order by salary desc //error
原因:order bywhere之後生效 

未完待續 接續子查詢

11 . 子查詢:嵌套查詢
I. 語法:與基本查詢相同。
II. 詳解:
將子查詢“一行一列”的結果作爲條件判斷做第二次查詢。
將子查詢“多行一列”的結果作爲枚舉查詢的判斷條件做第二次查詢。
將子查詢“多行多列”的結果作爲一張臨時表進行第二次查詢。
III. 用例:
//查詢工資大於平均工資的員工信息(工號,名字,薪資)
思路:

1). 先查詢員工平均薪資
select avg(salary) from employees; //平均薪資:6461.83177570093

2). 查詢大於平均薪資的員工信息
select employee_id , first_name , salary
from employees
where salary > 6461.83177570093 

SQLselect employee_id , first_name , salary
from employees
where salary > (select avg(salary) from employees) //一行一列,才能和salary進行比較
//查詢與姓氏爲‘King’的員工在同一部門的員工信息(工號,名字,薪資,部門id)
思路:
1). 先查詢 'King' 所在的部門編號
select department_id
from employees
where last_name = 'King' //部門編號:80、90

2). 再查詢8090號部門的員工信息
select employee_id , first_name , salary , department_id
from employees
where department_id in (80,90); 
SQL:
select employee_id , first_name , salary , department_id
from employees
where department_id in (select department_id cfrom employees where last_name = 'King'); //N行一列
//查詢員工表中工資排名前5員工的信息(編號,名字,薪資)
思路:
1). 先對所有員工的薪資進行排序(排序後的臨時表)
select employee_id , first_name , salary
from employees
order by salary desc

2). 再查詢臨時表中前5行員工信息
select employee_id , first_name , salary
from (臨時表) 
where rownum <= 5;
SQL:合併
select employee_id , first_name , salary
from (select employee_id , first_name , salary from employees order by salary desc) //N行N列
where rownum <= 5;

13 . 集合運算符:將多個查詢結果合併爲一張臨時表。
I. 語法:A查詢結果 集合運算符 B查詢結果。
II. 詳解:
union:並集。(聯合去重)
union all:並集。(聯合)
minus:差集。(減去)
intersect:交集。(較差)
III. 用例:
//查詢60、70號部門員工信息
select * from employees where department_id in (60,70);

//查詢70、90號部門員工信息
select * from employees where department_id in (90,70);

通過集合運算符將上面兩條SQL語句的查詢結果進行合併。

//union:並集將A和B的查詢結果合併,重複數據只保留一份。
select * from employees where department_id in (60,70)
union
select * from employees where department_id in (90,70); //70號部門的員工信息只保留一份。
//union all:並集將A和B的查詢結果合併,重複數據如數顯示。
select * from employees where department_id in (60,70)
union all
select * from employees where department_id in (90,70); //70號部門的員工信息有幾份保留幾份
//minus:差集在A中刪除與B中相同的結果。
select * from employees where department_id in (60,70)
minus
select * from employees where department_id in (90,70); //在A中刪除與B中出現的重複數據,70號部門

AAAA//intersect:交集只保留A和B中重複的數據。

select * from employees where department_id in (60,70)
intersect
select * from employees where department_id in (90,70); //只保留重複的70號部門的數據

注:使用規則【重點】:查詢結果可以來自與不同的表、不同的列。但列的個數必須相同、列的數據類型必須相同,最終結果中的列名和數據類型 依賴與第一個結果集。

14 . 表連接查詢:(Excel)

I. 語法:table1 連接 table2 on 連接條件

II. 詳解:
外連接:
左外連接:主表left [outer] join 從表
右外連接:從表right [outer] join 主表
全外連接:主表 full [outer] join 主表
內連接:從表 inner join 從表
自連接:主表 連接方式 從表 (主從相同)

III. 用例:
//查詢員工工號,名字,薪資,部門id,部門名稱
問題:當查詢數據來自於多張表時,使用表連接。
思路:將多張表基於關係列,拼接成一張大表,再取其中的某些列。

1). 左外連【重點】:table1 left join table2 on連接條件(table1爲主表)

//查詢所有員工信息,以及所對應的部門名稱(沒有部門的員工,也在查詢結果中)
select e.employee_id , e.first_name , e.salary , d.department_name
from employees e left join departments d on e.department_id = d.department_id;

2). 右外連【瞭解】:table1 right join table2 on 連接條件(table2爲主表)

//查詢所有部門信息,以及此部門中的所有員工信息(沒有員工的部門,也在查詢結果中)
select e.employee_id , e.first_name , e.salary , d.department_name
from employees e right join departments d on e.department_id = d.department_id;

3). 全外連【瞭解】:table1 full join table2 on 連接條件(兩張表都是主表)

//查詢所有員工信息和所有部門信息(沒有部門的員工、沒有員工的部門,都在查詢結果中)
select e.employee_id , e.first_name , e.salary , d.department_name
from employees e FULL join departments d on e.department_id = d.department_id;

4). 內連接【瞭解】:table1 inner join table2 on 連接條件(兩張表都是從表)

//查詢所有有部門的員工信息(既不包括沒有部門的員工,也不包括沒有員工的部門)
select e.employee_id , e.first_name , e.salary , d.department_name
from employees e inner join departments d on e.department_id = d.department_id;

5). 自連接【重點】:table1 連接 table2 on 連接條件(邏輯上的兩張表,物理上的同一張表)

//查詢員工編號,名字,直接領導(經理)id,直接領導的名字
select e1.employee_id , e1.first_name , e1.manager_id , e2.first_name
from employees e1 left join employees e2 on e1.manager_id = e2.employee_id;

6). 三表連接查詢:
--查詢所有員工工號、名字、部門名稱、部門所在國家ID

select * from employees e 
inner join departments d 
on e.department_id = d.department_id
inner join locations l
on d.location_id = l.location_id

交叉連接【瞭解】 cross join 乘積
select * from employees cross join departments
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章