1、union --可以把兩個不同的表的數據彙總到一個結果集中
select id from stud1 union select id from stud2; --這樣就會把stud1和stud2 的id都列出來,當然你就可以放到list去了。會自動排序,並且合併相同的記錄。
2、union all
作用跟union相同,但是不會自動排序,也不會合並相同記錄。
3、decode
decode的語法:
decode(value,if1,then1,if2,then2,if3,then3,...,else)
表示如果value等於if1時,DECODE函數的結果返回then1,...,如果不等於任何一個if值,則返回else。
select decode(sign(salary - 8000),1,salary*1.15,-1,salary*1.2,salary from employee;
表示:salary大於8000 返回1.15倍,如果小於的話爲1.2倍,否則返回salary的值。
4、sign
sign()函數根據某個值是0、正數還是負數,分別返回0、1、-1。
5、nvl
語法:NVL(eExpression1, eExpression2)
如果 eExpression1 的計算結果爲 null 值,則 NVL( ) 返回 eExpression2。如果 eExpression1 的計算結果不是 null 值,則返回 eExpression1。eExpression1 和 eExpression2 可以是任意一種數據類型。如果 eExpression1 與 eExpression2 的結果皆爲 null 值,則 NVL( ) 返回 .NULL.。
6.substr( string, start_position, [ length ] )
取子字符串,從start_position開始,取length個,length爲可選,如果length爲空則返回start_position後的所有字符。
例如: select substr('This is a test', 6, 2) from dual; would return 'is'。
start_position爲負數時,表示從字符串尾巴倒着數。
7.oracle case when的用法
SELECT col1, col2,
CASE
WHEN col3 > 1 AND col3 <2
THEN '1'
WHEN col3 > 2 AND col3 <3
THEN '2'
WHEN col3 > 3 AND col3 <4
THEN '3'
ELSE '4'
END mylevel
FROM table1
注意點:
1、以CASE開頭,以END結尾
2、分支中WHEN 後跟條件,THEN爲顯示結果
3、ELSE 爲除此之外的默認情況,類似於高級語言程序中switch case的default,可以不加
4、END 後跟別名
8.INSTR
INSTR(源字符串, 目標字符串, 起始位置, 匹配序號)
例如:INSTR('CORPORATE FLOOR','OR', 3, 2)中,源字符串爲'CORPORATE FLOOR', 目標字符串爲'OR',起始位置爲3,取第2個匹配項的位置。
默認查找順序爲從左到右。當起始位置爲負數的時候,從右邊開始查找。
所以SELECT INSTR('CORPORATE FLOOR', 'OR', -1, 1) "Instring" FROM DUAL的顯示結果是
Instring
——————
14