Oracle資料庫SQL總結

2021-12-30 00:33:36 字數 4698 閱讀 8943

1oracle時間段的查詢

1.1 場景:根據使用者輸入的時間段過濾出相應記錄。

1.2 解決辦法:

第一種寫法:

sql** 

select * from t_xjxx_xjgl  

where createdate >= to_date(2011-6-13, yyyy-mm-dd)  

and createdate <= to_date(2011-6-17, yyyy-mm-dd); 

select * from t_xjxx_xjgl

where createdate >= to_date(2011-6-13, yyyy-mm-dd)

and createdate <= to_date(2011-6-17, yyyy-mm-dd); 

第二種寫法:

sql** 

select * from t_xjxx_xjgl  

where to_char(createdate, yyyy-mm-dd) >= 2011-6-13 

and to_char(createdate, yyyy-mm-dd) <= 2011-6-16; 

select * from t_xjxx_xjgl

where to_char(createdate, yyyy-mm-dd) >= 2011-6-13

and to_char(createdate, yyyy-mm-dd) <= 2011-6-16;

第三種寫法:

sql** 

select * from t_xjxx_xjgl  

where createdate > to_date(2011-6-15, yyyy-mm-dd) - 1  

and createdate <= to_date(2011-6-16, yyyy-mm-dd) + 1; 

select * from t_xjxx_xjgl

where createdate > to_date(2011-6-15, yyyy-mm-dd) - 1

and createdate <= to_date(2011-6-16, yyyy-mm-dd) + 1;

取出當前時間在開始時間和結束時間範圍內的記錄:

sql** 

select * from t_xjxx_xjgl  

where 1 = 1  

and to_char(kssj, yyyy-mm-dd) <= to_char(sysdate, yyyy-mm-dd)  

and to_char(jssj, yyyy-mm-dd) >= to_char(sysdate, yyyy-mm-dd) 

select * from t_xjxx_xjgl

where 1 = 1

and to_char(kssj, yyyy-mm-dd) <= to_char(sysdate, yyyy-mm-dd)

and to_char(jssj, yyyy-mm-dd) >= to_char(sysdate, yyyy-mm-dd)

2 oracle建立觸發器的例子

2.1場景:建立t_xjxx_xjgl中bh欄位為自增長型別,start by 1 increment by 1

2.2解決方法:bh欄位的型別設定為number,建立sequence

sql** 

create sequence  seq_xjxx_bh    

minvalue 1 maxvalue 999999   

increment by 1 start with 11   

cache 10 noorder  nocycle ; 

create sequence  seq_xjxx_bh 

minvalue 1 maxvalue 999999

increment by 1 start with 11

cache 10 noorder  nocycle ; 建立觸發器:

sql** 

create or replace trigger tr_addid  

before   insert   on   t_xjxx_xjgl  

for   each   row  

begin 

if (:new.bh is null) then 

select seq_xjxx_bh.nextval into :new.bh from dual;  

end if;  

end; 

create or replace trigger tr_addid

before   insert   on   t_xjxx_xjgl

for   each   row

begin

if (:new.bh is null) then

select seq_xjxx_bh.nextval into :new.bh from dual;

end if;

end;

3 oracle中經常使用的函式

3.1場景:按照使用者設定的定時器時間段,過濾出資料後update下一執行時間。例如使用者設定是郵件傳送頻率為每月15號 12:00:00傳送,則計算出下一傳送時間點為當前傳送時間+1個月;設定頻率為每週一 09:00:00傳送,則計算出下一傳送時間+一周

3.2解決方法:

1)、add_months(x,y)函式,平時我使用的場景也就是對月份進行加減時使用add_months函式,其中y若是負整數表示對月份的減操作;y是正整數表示對x月份新增y個月

sql** 

select add_months(sysdate, -1) from dual 

select add_months(sysdate, -1) from dual2)、next_day(x,day)返回x日期下乙個day的日期,新手需要注意的是這裡的day指的是星期。1表示星期天、2表示星期一,以此類推。

sql** 

select next_day(sysdate, 2) from dual; 

select next_day(sysdate, 2) from dual; 返回從當前時間開始計算下一星期一的日期。

3)、如果是+1天或是-1天的操作可以直接對日期進行+-操作,like this

sql** 

select sysdate-1 from dual  

select sysdate-1 from dual oracle支援對日期進行運算,運算時是以天為單位進行的。

4)、last_day(x) 獲取x月份中的最後一天

5)、months_between(x,y);x>y返回正數,表示x和y之間相隔的月份數(實際專案中沒使用過,暫時不做太多記錄)。

字串處理函式:

6)、nvl(x,value),如果x is null 則返回value中的值,否則返回x;

7)、length(x),返回x的字元長度;

8)、substr(x,start,length) 擷取字串,對字串x進行擷取,從start開始擷取的長度為length;如果需要取字串的後面幾位可以這樣寫:

sql** 

select substr(test, -2) from dual; 

select substr(test, -2) from dual; 返回最後兩位字元 st

4、oracle中偽列的使用技巧

4.1場景:利用oracle中的偽列進行分頁是一種簡單方便的分頁手段有些場景中我們也可以使用偽列來代替組函式,從而巧妙的實現過濾要求。

1 )  利用偽列取出工資最高的第6到第10名雇員的記錄

sql** 

select *  

from (select rownum rn, temp.*  

from (select e.ename, e.sal  

from emp e  

where rownum <= 10  

order by e.sal desc) temp)  

where rn > 5 

select *

from (select rownum rn, temp.*

from (select e.ename, e.sal

from emp e

where rownum <= 10

order by e.sal desc) temp)

where rn > 5

2 ) 利用偽列進行分頁操作:

sql** 

select b.*, rn  

from (select a.*, rownum as rn2  

from (select xxkc.wid,  

...  

rownum as rn  

from t_py_xxkc xxkc  

left join t_py_kc kc on kc.kcdm = xxkc.xxkcdm  

order by xxkc.xxkcdm) a) b  

where b.rn2 >  v_start   

and b.rn2 <=  (((v_start/10) + 1) * v_limit ) 

select b.*, rn

from (select a.*, rownum as rn2

from (select xxkc.wid,

...rownum as rn

Oracle資料庫SQL總結

1oracle時間段的查詢 1.1 場景 根據使用者輸入的時間段過濾出相應記錄。1.2 解決辦法 第一種寫法 sql select from t xjxx xjgl where createdate to date 2011 6 13 yyyy mm dd and createdate to dat...

Oracle資料庫SQL總結

1oracle時間段的查詢 1.1 場景 根據使用者輸入的時間段過濾出相應記錄。1.2 解決辦法 第一種寫法 select from t xjxx xjgl where createdate to date 2011 6 13 yyyy mm dd and createdate to date 20...

資料庫 Sql總結

關係型資料庫 mysql oracle db2 sqlserver 非關係型資料庫 redis mongodb a 關係型資料庫 是指採用了關係模型來組織資料的資料庫。關係模型指的就是二維 模型,而乙個關係型資料庫就是由二維表及其之間的聯絡所組成的乙個資料組織。優點 可以使用sql語言便捷的運算元據...