資料庫的多表連線,自連線,樹狀結構查詢
1.左連線(left join)
例如:select * from a left join b on (a.id= b.id and a.level = '1' ) =========>a 表資料全部顯示
2.右連線(right join)
例如:select * from a right join b on (a.id= b.id and a.level = '1') =========>b 表資料全部顯示
3.全表連線(full join)
例如:select * from a full join b on (a.id= b.id and a.level = '1') =========>a和b 表資料全部顯示
4.(+)的用法(+放在哪邊,表示左或右連線)
例如:select * from a, b where a.id= b.id(+) =====>a表的資料全部顯示;
select * from a, b where a.id(+)= b.id =====>b表的資料全部顯示.
5.(,)逗號連線:表示等值連線
例如:select * from a, b where a.id= b.id =======>a表與b表匹配上的才能查出來
連線總結:
1-4:都是非等值連線,只有5是等值連線
6.表連線的on與where的用法
1)on : 兩個表通過on 處的條件連線成新的表
2) where : 針對from 形成的新表,對新表進行條件篩選
3) 例如 :select * from a left join b on (a.id= b.id and a.level = '1' and b.level = '2') where a.name = 'lily';
a和b通過on條件形成新的臨時表(ab),where後的條件是針對臨時表ab作進一步的限制。
7. 等值樹狀結構與非等值的樹狀介面
1)等值樹狀結構
select distinct s1.FREIGHT_AREA_NAME 總部名稱, s1.FREIGHT_AREA_CODE 總部編碼,
s2.FREIGHT_AREA_NAME 省份名稱, s2.FREIGHT_AREA_CODE 省份編碼,
s3.FREIGHT_AREA_NAME 市名稱, s3.FREIGHT_AREA_CODE 市編碼,
s4.FREIGHT_AREA_NAME 區縣名稱, s4.FREIGHT_AREA_CODE 區縣編碼
from FREIGHT_AREA s1, FREIGHT_AREA s2,
FREIGHT_AREA s3, FREIGHT_AREA s4
where s1.freight_area_code = s2.parent_area_code
and s2.freight_area_code = s3.parent_area_code
and s3.freight_area_code = s4.parent_area_code
and s1.REC_STATUS = '1' and s2.REC_STATUS = '1' and s3.REC_STATUS = '1' and s4.REC_STATUS = '1'
and s1.AREA_LEVEL in ('1')
and s2.AREA_LEVEL in ('2')
and s3.AREA_LEVEL in ('3')
and s4.AREA_LEVEL in ('4')
order by s1.FREIGHT_AREA_CODE, s2.FREIGHT_AREA_CODE, s3.FREIGHT_AREA_CODE, s4.FREIGHT_AREA_CODE
2)非等值的樹狀介面
select distinct s2.cname 省名稱
s2.ts_bm 省編碼
s3.cname 市名稱
s3.ts_bm 市編碼
s4.cname 區名稱,
s4.ts_bm 區編碼
from system_city s1
right join system_city s2 on (s1.cname = s2.upcname and s1.ts_level = '0' and s2.ts_level = '1')
full join system_city s3 on (s2.ts_bm = s3.ts_up_bm and s3.ts_level = '2')
left join system_city s4 on (s3.ts_bm = s4.ts_up_bm and s4.ts_level = '3')
where s2.ts_level = '1'
order by s2.ts_bm, s3.ts_bm, s4.ts_bm
3)start with connect by prior(專門用於查詢樹狀結構)
層次化查詢,即樹型結構查詢,是SQL中經常用到的功能之一,通常由根節點,父節點,子節點,葉節點組成,其語法如下:
SELECT [LEVEL] ,column,expression,...
FROM table_name
[WHERE where_clause]
[[START WITH start_condition] [CONNECT BY PRIOR prior_condition]];
LEVEL:偽列,用於表示樹的層次
start_condition:層次化查詢的起始條件,指定階層的根。
prior_condition:定義父節點和子節點之間的關係,PRIOR指定父節點。作為運算子,PRIOR和加(+)減(-)運算的優先順序相同。condition ... PRIOR expr = expr 或者 ... expr = PRIOR expr
例如:
CONNECT BY last_name != 'King' AND PRIOR employee_id = manager_id ...
CONNECT BY PRIOR employee_id = manager_id and PRIOR account_mgr_id = customer_id SYS_CONNECT_BY_PATH
SYS_CONNECT_BY_PATH這個函式是oracle9i才新提出來的!它一定要和connect by子句合用!第一個引數是形成樹形式的欄位,第二個引數是父級和其子級分隔顯示用的分隔符!
(一)當前節點遍歷子節點:
select
dp_code,
max
(sys_connect_by_path(dp_code,
'->'
)),
level
from
dp_mstr
where
1=1
--and dp_code = '商用結構部'
--and level > 1
start
with
dp_code=
'商用科技公司'
connect
by
prior
dp_code = dp_upper_dept
group
by
dp_code,
level
order
by
level
desc
;
(二)當前節點遍歷根節點:
select
dp_code,
max
(sys_connect_by_path(dp_code,
'->'
)),
level
from
dp_mstr
where
1=1
--and dp_code = '商用結構部'
--and level > 1
start
with
dp_code=
'商用科技公司'
connect
by
prior
dp_upper_dept = dp_code
group
by
dp_code,
level
order
by
level
desc
;
總結:
若當前節點遍歷子節點,則prior應該放在子節點一側;
若當前節點遍歷根節點,則prior應該放在上級節點一側。