標籤:包括 acl 定義 union 運算式 div round sele class
2.用SQL進行多表查詢
(1)無條件多表查詢
笛卡爾集:總記錄數=table1記錄數×table2記錄數
select * from table1, table2
(2)等值串連
內串連:select tab1.f_z, tab2.* from table1 tab1, table2 tab2 where tab1.f_z = tab2.f_c。
左外串連(包括沒有單位的機型):select tab1.f_z, tab2.* from table1 tab1, table2 tab2 where tab1.f_z = tab2.f_c(+)。
右外串連(包括沒有機型的單位):select tab1.f_z, tab2.* from table1 tab1, table2 tab2 where tab1.f_z(+) = tab2.f_c。
實際使用中,建議外串連統一使用左外串連;查詢欄位和串連條件中,若兩表都有相同的欄位名,必須指定欄位對應的表名。
3.用SQL進行巢狀查詢
也叫子查詢,子查詢形成的結果又成為父查詢的條件。
in、exists:如果子查詢得出的結果集記錄較少,主查詢中的表較大且又有索引時應該用in;反之如果外層的主查詢記錄較少,子查詢中的表大,又有索引時使用exists。
not in、not exists:不推薦not in,會導致兩個表的全表掃描,盡量轉換為minus重寫;not exists效率一般比較高;根據實際執行計畫調試。
union(並集):select 1 as f_id, ‘是’ as f_m from tab union select 0 as f_id, ‘否’ as f_m from tab。
intersect(交集)、minus(差集)
並、交和差操作的巢狀查詢要求屬性具有相同的定義,包括類型和取值範圍。
4.用SQL進行函數查詢
(1)函數:
Round、Count、Sum、Avg、Min、Max
(2)條件運算式:nvl、decode、case when
select nvl(f_z, 0) from table;如果f_z為空白,返回0;否則為f_z。
select decode(f_z, 1 , ‘客機‘, ‘貨機‘) from table。
select case when f_z = 1 then ‘客機‘ else ‘貨機‘ end from table。
case語句在處理相似問題就顯得比較簡捷靈活。另外,當需要匹配少量數值時,選用decode會更加方便一些。
5.Dual的使用
Dual是Oracle中實際存在的一種表,任何使用者都可以使用,常用在沒有目標表的select語句中。可用於查看時間、使用者、計算等;select user from dual;select sysdate from dual。
Oracle資料庫學習(二)