oracle数据库常用的语法与复合函数
oracle查用到一些復合函數以及一些常用的方法用來快速查詢數據,以下是我收集的一下常用方法,推薦給大家:
1、 當分組之后,針對某一屬性值進行合并并以逗號進行分割:
(1)所有版本都可使用:
- 合并數據:使用wm_concat(column)函數進行數據合并,以分組的形式將同一人的權限進行合并展示在一個屬性值中,結果為集合形式clob;
select u_id, wmsys.wm_concat(goods) goods_sum from shopping group by u_id - 切割數據:合并數據后將clob形式轉換為字符串形式,使用dbms_lob.substr(字段,長度,起始位置)方法將clob轉換為varchar2類型 ;
select dbms_lob.substr(t.text,4000,1) from jgw_wblk t where t.createdate between to_date(‘2015-07-01’,‘yyyy-mm-dd’) and to_date(‘2015-07-31’,‘yyyy-mm-dd’)
(2)oracle11g版本之后可使用:推薦方法
- 使用listagg() within group()函數
例:select listagg(name, ‘,’) within group( order by name) as name from user;
2、查詢所在節點所有子節點信息,以樹的形式呈現
- 層次化查詢—oracle特有功能:
select * from t_user start with id=xx connect by prior id=parent_id
3、指定屬性在為空的情況下修改,不為空時值不變
(1)nvl函數
- 例:update tb_user set age=nvl(age,‘xxx’) where name=‘tt’;
age為空時修改為xxx,不為空時不變
(2)使用case when …then…else…end
- 例:update ss
set bengtime = case when bengtime is null then sysdate else bengtime end,
endtime = case when endtime is null then sysdate else endtime end
where id = …
(3)with … as …select 臨時表
- 例:with buyer as (select m.no,count() cout from tb_buyers m group by m.no having count(m.no)>500) select s.no,b.cout from tb_ss s,buyer b where s.no=b.no and s.end_date>=to_date(‘2021-02-04 00:00:00’,‘yyyy-mm-dd hh24:mi:ss’);
—等價于
select b.no,b.cout from tb_ss s,(select m.no,count() cout from tb_buyers m group by m.no having count(m.no)>500) b where b.no=s.no and s.end_date>=to_date(‘2021-02-04 00:00:00’,‘yyyy-mm-dd hh24:mi:ss’);
4、另外,常用到日期形式的查詢條件,因此涉及類型轉換問題,如:
(1)Between …… and …… + 日期的date和varchar2轉換
- 例:Select * from tb_user u where u.update_date between to_date(“2020-11-18 00:00:01”,”yyyy-mm-dd hh24:mi:ss”) and to_date(“2020-11-18 23:59:59”,”yyyy-mm-dd hh24:mi:ss”);
Select * from tb_user where to_char(start_end,’yyyy-mm-dd hh24:mi:ss’)>=’2019-01-01 00:00:01’ and to_char(end_date,’yyyy-mm-dd hh24:mi:ss’) < =’2019-12-31 23:59:59’
(2)日期相減:
- 例:Select ceil(to_date(“2020-11-18 00:00:01”,”yyyy-mm-dd hh24:mi:ss”)- to_date(“2020-11-18 00:00:01”,”yyyy-mm-dd hh24:mi:ss”)) from dual;
Select round(to_number(sysdate- to_date(“2020-11-18 00:00:01”,”yyyy-MM-dd hh24:mi:ss”))) from dual;
5、一些常用的sql語法推薦給大家
(1)解壓縮表
解壓縮表:alter table 表名 move nocompress;
索引失效:alter index 索引 rebuild online;
(2)常用的ddl語句
增加一個列:alter table 表名 add (字段名 數據類型); 括號可加可不加
增加多個列:alter table 表名 add (字段名1 數據類型,字段名2 數據類型,……);
刪除一列:alter table 表名 drop column 字段名;
刪除多列:alter table 表名 drop column (字段名1,字段名2,……);
重新定義某列:alter table 表名 modify 字段名 數據類型;
定義表名注釋:comment on table表名is “注釋”
定義列名備注釋:comment on column表名.列名 is “注釋”
(3)序列
創建序列:create sequence seq_sys_user minvalue 1 maxvalue 999999999999999999 start with 1 increment by 1 nocache
語法:
- create sequence 序列名
start with 2 –從2開始
increment by 3—每次加3
nomaxvalue—沒有最大值
minvalue 1—最小值1
nocycle—不循環
nocache;–不緩存 **
使用序列插入數據庫表:insert into sys_user(id,user_name,password) values(seq_sys_user.nextval,1,11);
查詢所有已經建好的序列:select * from user_sequences;
查詢seq_sys_user 的下一個序列:select seq_sys_user.nextval from dual;
查詢當前序列:select seq_sys_user.currval from dual;
刪除序列:drop sequence seq_sys_user;
修改序列:alter sequence seq_sys_user increment by 79;
總結
以上是生活随笔為你收集整理的oracle数据库常用的语法与复合函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: @requestparam @param
- 下一篇: linux cmake编译源码,linu