Oracle 常用查询语句
1. 查询数据必备SELECT
1、select语法
select 字段 from 表名 where 条件
group by
having
order by
2、获取指定字段的数据
select 字段1,字段2 from 表名
3、获取所有字段的数据
select * from 表名
4、使用别名替代表中的字段名
select t.* from 表名 t
5、使用表达式操作查询的字段 (对查询结果进行相应计算)
select 产品价格*1.2 from 表名 t
6、使用函数操作查询的字段
select substr (taname,1,2) from rtb.rds_ta ;
7、去除检索数据中的重复记录
select distinct(字段名) from 表名;
2. 排序
1、排序的语法(只能放在最后)
asc:升序(默认升序)
desc:降序
2、使用升序和降序来处理数据
select * from 表名 order by asc(或者 desc)
3、排序时对NULL值的处理
默认为最大值,升序在最后,降序在首位
如果升序时,想把null放在首位:
select * from 表名 order by 字段名 nulls frist
如果降序时,想把null放在最后:
select * from 表名 order by 字段名 nulls last
EG : select t.END_TIME_,t.* from act_hi_procinst t order by t.END_TIME_ nulls first;
4、使用别名作为排序字段
select 产品价格*1.2 from 表名 order by 产品价格*1.2 desc
5、使用表达式作为排序字段
6、使用字段的位置作为排序字段
select 字段1,字段2 from 表名 order by 2 desc
select t.customerno,t.insertdate from ua.acct_trans t order by 2 desc
7、使用多个字段排序
select 字段1,字段2 from 表名 order by 1 desc , 2 desc
select t.insertdate,t.customerno from ua.acct_trans t order by 1 desc , 2 desc;
3. where
1、查询中使用单一条件限制
select * from 表名 where 某字段满足什么条件;
select t.insertdate,t.customerno from ua.acct_trans t where t.insertdate>20170716;
2、查询中使用多个条件限制
and :且,or 或,between and
select t.insertdate,t.customerno from ua.acct_trans t where t.insertdate>20170701 and t.insertdate <20170710 ;
select t.insertdate,t.customerno from ua.acct_trans t where t.insertdate between 20170701 and 20170710;
3、模糊查询数据
like : like ‘% %’
select * from cif.cif_visitor_apply t where t.mobiletel like '187%';
4、查询条件限制在某个列表范围之内
in:在某个范围内
not in :不在此范围内
select t.insertdate,t.customerno from ua.acct_trans t where t.insertdate in (20170713,20170718);
select t.insertdate,t.customerno from ua.acct_trans t where t.insertdate not in (20170713,20170718);
5、专门针对NULL值的查询
null: is null
not null: is not null
4. GROUP
用于分组查询
1、GROUP BY子句语法及使用
2、HAVING子句的使用
select 函数 from 表 group by 列名 having 列名满足条件
select avg(t.transferamt) from fms.pay_app t group by t.transferamt having t.transferamt>10000;
5. 子查询(嵌套查询)
1、子查询返回单行
select * from 表1 where 列名1 in (select 列名1 from 表2 where 列名n);
select * from rtb.acct_fund t where t.customerno in (select t2.customerno from rtb.ack_acct t2 where t2.investorname='公孙巧枚');
2、子查询返回多行
select price from product where price < all(select price from product where fundcode =’’); --查看比指定价格还低的产品
6. 连接
1、最简单的连接查询:一张表的一行与另一张表的一行连接形成新表
2、内连接:两张表查出匹配的数据,匹配不了的查询不到(等值连接、非等值连接)
select 表1列1,表2列2 from 表1,表2 where 表1某列 = 表2某列;
EG: select a.customerno,b.mobiletel from cif.cif_visitor a, cif.cif_visitor_apply b where a.mobiletel=b.mobiletel;
3、自连接:自己和自己匹配比较
select * from 表1,表1 where 表1列1 = 表1列1 and 表1列2 ! = 表1列2;
4、外连接
左连接、右连接、全连接
常用查询
1、查询一段时期内的天数
select to_days('20170822') - to_days('20170916')
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号