Oracle 简单查询小点积累

select  column from table where condition

select :查询列
* :通配符,查询所有列
|| :连接列或者字符串
distinct :去重列,获取列中唯一值 tip:会导致索引失效 降低查询效率
空格 或者 as : 别名

where:查询条件
’ ’ 中的字符串区分大小写
to_date(‘2018-01-01’,’yyyy-mm-dd’) :字符串转日期格式
to_char(date,’yyy-mm-dd’) :日期格式转字符串

范围操作符:
between 100 and 200 相当于 >=100 and <= 200
in(100,200,300) 相当于 =100 or =200 or =300

like:通配符,模糊字符串搜索
%a:以a结尾所有字符 %表示零个或多个字符
-a :第二个字符为a的所有字符 _表示单个字符

判断空值:
is null is not null

逻辑组合: 按优先级排序
not > and > or

排序:order by
asc :升序
desc :降序

函数
单行函数;
initcap (name) :转换首字母大写
concat(a,b) :连接两个字符串a ,b 的内容
round(num) :四舍五入
nvl(a,0) :如果a为空 返回0
分组函数:
sum(a) : 返回a这列的数据和
avg(a) :返回a这列字段的平均值
min(a) :返回a这列字段的最小值
max(a) : 返回a这列字段的最大值
count() :返回记录条数
*

顺便讲下count(1) 与 count(*) 与count(a)的区别
count(1) :查询表中第一列非null数据行数
count(*):扫描全表所有列,即返回表所有行数
count(a)  :返回a字段中非null数据行数,  a越靠后查询速度越慢
所以一般情况:查询速度: count(1) >count(a) >count(*)*

分组:group by
约束分组条件 :having

--查询每个人 不同险种下多少条记录
select name,riskcode,count(1),sum(plan) --除了分组函数参数的列,其他查询列都必须在组函数中
from pandaa  
group by name,riskcode --按名字和险种分组  
having  sum(plan) between 10 and 60 --分组函数作为查询条件放到havingwhere中不允许使用分组函数
order by count(1) --按记录数升序排序

来个用到所有小点的例子:

select distinct grpname  公司名称,--去重  获取name列中唯一值
'该 公 司 属 于 '||(select codename from ldcode where businesstype = code and codetype = 'businesstype') ||'业务类别' 行业类别,--使用||拼接字符串
sum((peoples*2)) as  两倍员工数量, -- 查询字段可以为表达式     as 为查询列起别名  可以直接用空格
min(to_char(makedate,'dd-mm-yyyy')) 入机日期,--date类型转字符串且设置显示格式
max(to_date('2018-04-10','yyyy-dd-mm')) 当前日期 --字符串转为日期格式    oracle默认显示dd-mm-yyyy格式
from ldgrp
where --指定查询条件
grpname is not null --指定公司名称不为空
and asset >0 --总资产额大于零
and grpnature between 01 and 10 --范围操作符  相当于 grpnature>=01 and grpnature <=10
and businesstype in ('01','02','03','04','05','06','07','08','09','10','11','12') --相当于businesstype = '01'
-- or businesstype='02'...
and grpname like '%熊猫%' 
or grpname like'%panda%'--公司名称包含熊猫 或者panda的
group by grpname,businesstype  --按公司名称分组    除了grpname,businesstype两列其他查询列必须是组函数

having sum(asset) >200--组函数作为查询条件只能放havingorder by 两倍员工数量 desc --按员工总数降序排列

查询结果:
pandagogogo 该 公 司 属 于 金融、保险业业务类别 666666 04-05-2017 2018/10/4
panda爱美丽 该 公 司 属 于 金融、保险业业务类别 133332 28-09-2017 2018/10/4
熊猫企业GOGOGO 该 公 司 属 于 金融、保险业业务类别 6666 25-12-2017 2018/10/4
熊猫企业 该 公 司 属 于 农、林、牧、渔业业务类别 1332 20-04-2017 2018/10/4

猜你喜欢

转载自blog.csdn.net/fightingitpanda/article/details/79877960