2016.9.9《Oracle查询优化改写技巧与案例》电子工业出版社一书中的技巧
1、coalesce (c1,c2,c3,c4,...) 類似于nvl但可以從多個表達式中返回第一個不是null的值
2、要在where條件中引用列的別名,可以再嵌套一層查詢
select * from ( select salary gz from person) where gz>100
3、like()函數的通配符除了%號外,還有_代表一個字符,若要在like里表達_符號需轉義:
like('\_BCD') escape '\' escape 用來定義轉義符,此時可以寫'\\'代表真正的'\'符號
4、order by 1或2等數字 表示按第幾列排序 (count(1)是不是也類似?)
5、substr(aa,-3)表示取倒數后3位,但不適用于指定起始位置,例如不能用substr(aa,3,-2)
6、排序時控制空值記錄排在前或在后: order by xx nulls first(nulls last)
7、with.. as... 創建只在語句執行時存在的臨時視圖
with e as (select ad_hp_id id, code_id code , txt_name name from ad_hp where code_id like 'ZB%')
select id,code, name from e
8、left join的where、order 語句可寫在on語句之后
9、內連接相當于直接where兩表字段相等,只有兩表都匹配的數據才出現
全連接兩張表所有記錄都出現,不匹配的項顯示為null
左右連接可以像正常兩表用where關聯方式寫,只不過在右表條件前加上(+):
select a.code_id,b.* from ad_hp a, bureau b
where a.code_id LIKE 'ZB%'
and a.txt_name_admin = b.bureau_code(+)
ORDER BY a.code_id
10、11g以后增加了統計字符串中的某字符數的函數regexp_count('ababcabcd','a') ,結果=3
11、按某列排序后,取它最大或最小值對應的其它列 max(col1) keep(dense_rank first或last order by col2)
如果要在分組內部統計,可用 over(partition by col3)。也可用group by 分組
select max(code_id) keep(dense_rank first order by geo_lat) from ad_hp t where code_id like 'ZB%'
12、在樹型數據結構中,列舉某一節點所有后代節點(第12章)
select empno as 員工編碼, ename as 姓名,mgr as 主管編碼,(prior ename) as 主管姓名,sys_connect_by_path(ename,',') as 路徑
from scott.emp
start with empno=7566
connect by (prior empno)=mgr
轉載于:https://www.cnblogs.com/mol1995/p/5965023.html
總結
以上是生活随笔為你收集整理的2016.9.9《Oracle查询优化改写技巧与案例》电子工业出版社一书中的技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序进入后台继续执行
- 下一篇: Intellij idea genera