oracle一些函式的簡單應用

2021-09-30 15:02:39 字數 1843 閱讀 1007

[b]一》、select sysdate +/- time from dual[/b]

sysdate+(5/24/60/60) 在系統時間基礎上延遲5秒

sysdate+5/24/60 在系統時間基礎上延遲5分鐘

sysdate+5/24 在系統時間基礎上延遲5小時

sysdate+5 在系統時間基礎上延遲5天

add_months(sysdate,-5) 在系統時間基礎上延遲5月

add_months(sysdate,-5*12) 在系統時間基礎上延遲5年

[b]二》、某年/月/周的第幾天[/b]

ddd 當年第幾天 (2023年5月29日為2023年第149天)

sql> select to_char(sysdate,'ddd') from dual;

dd 當月第幾天

sql> select to_char(sysdate,'dd') from dual;

d 週內第幾天

sql> select to_char(sysdate,'d') from dual;

[b]

三》、select case when 及case when 搜尋 用法[/b]

selecct case salary when 1000 then 'low'

when 5000 then 'hign'

else 'middle' end [sal_level別名]

from test;

select case when salary<=1000 then 'low'

when salary>5000 then 'hign'

else 'middle' end [sal_level別名]

from test;

詳情請參:

[b]四》、decode用法(作用等同於if,elsif,else,end if)[/b]

decode(條件,值1,顯示值1,值2,顯示值2,…… 值n,顯示值n)

decode(條件,值1,顯示值1,值2,顯示值2,…… 值n,顯示值n,預設值)

select decode(salary,1000,1,50000,3) from test;

select decode(salary,1000,1,50000,3,2) from test;

[b]五》、exists,in的用法[/b]

in 是把外表和內錶作hash join,而exists是對外表作loop,每次loop再對內表進行查詢。

a為小表[外表] b為大表[內錶] (高效率)

select id,name from a where exist (select * from b where a.id=b.aid)

這個時候查詢它用到了b表上id列的索引(因為外表小了,遍歷的行次數就少了很多),所以效率高

若改為in,則用到了a表上cc列的索引,但是因為a是小表,而且大表b上的索引也沒有充分利用,因此效率不高

a為大表 b為小表 (高效率)

select id,name from a where id in (select id from b)

in 是把外表和內錶作hash join,而exists是對外表作loop,每次loop再對內表進行查詢。

not in 和not exists

如果查詢語句使用了not in 那麼內外表都進行全表掃瞄,沒有用到索引;

而not extsts 的子查詢依然能用到表上的索引。

所以無論那個表大,用not exists都比not in要快。

也就是說,in和exists需要具體情況具體分析,not in和not exists就不用分析了,盡量用not exists就好了

詳情參: exists的用法

oracle函式的一些簡單例子

數值型函式 select ceil 12.3 from dual 大於或等於n的最小整數 select floor 12.3 from dual 小於等於n的最大整數 select mod 15,2 from dual 取餘 select power 2,4 from dual 2的4次方 sele...

一些oracle函式

使用case表示式直接在select語句中執行條件邏輯 select ename,sal,case when sal 2000 then underpaid when sal 4000 then overpaid else ok end as status from emp coalesce函式用實...

php memcache的一些簡單應用

例項化memcache類的物件 memcache new memcache 連線本機的memcached伺服器 memcache connect localhost 11211 向本機的memcached伺服器中新增一組資料 is add1 memcache add brophp brophp框架 ...