sql语句书写与阅读要点:
1>分结构阅读与书写。 拆分阅读,拆分书写再组合,由简到繁。
Oracle基础数据类型:
4类:
字符型:字符串
char(最大2000), nchar(最大1000, 支持Unicode)--->固定长度
varchar2(最大4000), nvarchar2(最大2000, 支持Unicode)--->可变长度
数字类型
包括整数和小数
number(有效数字, 总位数);
float()--->存储二进制类型的数据, 1-126位0.30103
日期类型:时间
date:取值范围:公元前4712年1月1号---公元9999年12月31号, 可以直接精确到秒
timestamp(时间戳: 更精确的数据, 可以精确到毫秒)
其它类型:大文本, 文件
blob: 最大4G, 以二进制的形式来存放数据
clob: 最大4G, 以字符串的形式存放
Oracle运算符和表达式
表达式类似于java, 可以是常量, 可以是变量, 也可以是表中的一个字段
算术运算符(+ - * /)
比较运算符(> >= < <= = <>/!=)
逻辑运算符(and or not)
字符串连接符(||)
常用关键字
insert into----insert into 表名 values ();
delete-----DELETE 表名 WHERE 列名 运算符 条件
update----UPDATE 表名 SET 列名=值 WHERE 限制条件
select---尽量不要写*
select into
where
in
like(%, _)---模糊查询,%表示0个或多个;_表示一个;
order by----SELECT * FROM emp ORDER BY deptid DESC, ID DESC(排序,加上desc成倒序)
group by--(group by 增强)-------结合分组函数使用 SELECT deptid FROM emp GROUP BY deptid
having
set
distinct----去除一列中的重复;
between...and(闭合区间, 即包括前面的数, 也包括后面的数)
all-------SELECT * FROM emp e WHERE e.intime>=ALL(SELECT e1.intime FROM emp e1);
asc,desc
union all与union
-----
select jkhtbh, jkrxm, jkrzjh, htdkje, b.mc from grdk_sq_htxx a, bm_khjg b where jkhtbh in (select jkhtbh from grdk_dk_zz where dkffrq >= to_Date('20180101', 'yyyymmdd') and grbh in (select grbh from grdk_Sq_htxx where jkhtbh in (select jkhtbh from grdk_dk_zz where dkffrq < to_Date('20180101', 'yyyymmdd')) union select grbh from grdk_Sq_gtjkr where jkhtbh in (select jkhtbh from grdk_dk_zz where dkffrq < to_Date('20180101', 'yyyymmdd')))) and a.jgbm = b.bm; select jkhtbh, count(jkhtbh) ts, sum(dkye) je from (select jkhtbh, dkye from grdk_dk_zz where dkffrq <= to_date('20171231', 'yyyymmdd') union all select jkhtbh, sum(bjje + zczyqbjje) dkje from grdk_dk_hkmx where jkhtbh in (select jkhtbh from grdk_dk_zz where dkffrq <= to_date('20171231', 'yyyymmdd')) and ywfsrq > to_date('20171231', 'yyyymmdd') group by jkhtbh ) where dkye <> 0 group by jkhtbh;
insert into tmp_hfb_jkr_hkmx_cx (id,hkrq,pzbh,hkny,hkbj,hklx,hkfx,dkje,zlx,dkye,lxye,jkr,sfzh,hkzy,zqgjj,yhkkje,jkrzqe,pozqe) select 0,a.dkffrq ,a.rzywlsh,' ',0,0,0,a.dkffe,a.dqzlx,0,0,b.jkrxm ,b.jkrzjh ,'贷款发放' hkzy,0,0,0,0 from grdk_dk_zz a inner join grdk_sq_htxx b on a.jkhtbh=b.jkhtbh where a.jkhtbh='09008770' and dkffrq is not null union all select c.id,ywfsrq,ywlsh,hkqs,bjje+ZCZYQBJJE,lxje+YQLXJE,fxje,0,0,0,0,' ',' ',nvl(d.mc,' ') hkzy,gjjzqe,bjje+lxje+fxje+wyj+ZCZYQBJJE+YQLXJE-gjjzqe,jkrzqe,pozqe from grdk_dk_hkmx c left join bm_gd_hkzt d on d.bm=c.hkzt where jkhtbh='09008770' and ywfsrq is not null and hkzt in ('01','02','03','04') and trim(bczywlsh) is null order by c.id;
exists
----
select * from grdk_tx_hk_yh a where hkrq=date '2018-05-01' and swtyhbm like '0103' and ykbxhj=gjjtqje and exists( select * from grdk_dk_hkmx where jkhtbh=a.jkhtbh and ywlsh='218051601_0002268866' );
select nvl(sum(1),0) into v_cnt from dual where exists(select crbh from cr_jbxx_bg where crbh=v_dwbh and spzt='0' and bgxmdm='jcblbm'); if v_cnt<>0 then v_msg:='此单位已录入单位缴存比例批量调整清册!'; return ; end if;
select '单位编号:【'||a.dwbh||'】单位经济类型值:【'||a.dwjjlx||'】' from cr_dw a
where not exists(select bm from bm_dwjjlx b where b.jgbm like '01'||'%' and b.bm=a.dwjjlx) and a.jgbm like '01'||'%'
select count(*) from cr_gr a where f_sfz(a.zjhm)<>1 and a.zjlx='01'
and exists(select 1 from gjzf_gr_zz where grzhzt in('01','02') and grbh=a.grbh);------------exists和in的类比
select count(*) from cr_gr a where f_sfz(a.zjhm)<>1 and a.zjlx='01'
and a.grbh in(select grbh from gjzf_gr_zz where grzhzt in('01','02') and grbh=a.grbh);------------exists和in的类比
inner join,left outer join,right outer join
case...when...then...else ---SELECT CASE E.SSEX WHEN 1 THEN '男' WHEN 0 THEN '女' ELSE '不详' END bb FROM EMP E;
----
insert into tmp_hfb_jcdw_jkdzhd_cx (id, jyrq, skyhmc, skyhbm, yhzhhm, zhaiyao, fse, skzhhm, skzhmc, yhjslsh, beizhu, dwzh, dwmc, fkzhhm, fkzhmc, fkyhmc, fkyhbm, lsztmc, lsztbm) select a.id, jysj, nvl(b.mc, ' ') yhmc, a.yhbm, a.yhzhhm, zhaiyao, fse, skzhhm, skzhmc, yhjslsh, ' ', nvl(d.dwzh, ' ') dwzh, nvl(c.dwmc, ' ') dwmc, fkzhhm, fkzhmc, nvl(e.sszh, ' ') skzhhm, nvl(f.mc, ' ') skzhmc, case when hdid = 0 then '未核对' else '已核对' end, case when hdid = 0 then '01' else '02' end from im_yhzh_yemx a left join bm_yhbm b on a.yhbm = b.bm and b.jgbm like '01%' left join cr_dw c on a.fkzhmc = c.jbzhkhmc and (a.fkzhhm = c.jbzhkhyh or a.fkzhhm = c.jbzhyhzh2 or a.fkzhhm = c.jbzhyhzh3 or a.fkzhhm = c.jbzhyhzh4) left join gjzf_dw_zz d on d.dwbh = c.dwbh left join im_zhsz_dw e on a.fkzhhm = e.yhzhhm and a.fkzhmc = e.yhzhmc left join bm_sszh f on e.sszh = f.bm where jysj between v_ksrq and v_jsrq and fse between v_ksjyje and v_jsjyje and fsefx = '01' and a.yhbm like v_yhbm;
select dkffe 贷款发放额, (case when dkhkfs='01' then '等额本息' when dkhkfs='70' then '等本等息' when dkhkfs='02' then '等本本金' when dkhkfs='03' then '一次还款付息' when dkhkfs='03' then '自由还款方式' end) as dkhkfs from grdk_dk_zz;