sql/排序/函数

sql-2

sql-2

排序

排序用order by ,排序是对获取的集合进行排序,而不是改变存储结构顺序

  • 升序排序: asc(默认)

    --默认升序所以可以不写asc
    select* from emp order by sal;
    select* from emp order by sal asc;
    

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I2PZ041S-1615986893285)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615978710041.png)]

  • 降序排序: desc

    seletc * from emp order by sal desc;
    

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rXurOZDI-1615986893288)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615978824262.png)]

  • 多字段:在前面字段相等时,使用后面的字段排序
select ename, sal, comm
  from emp
 where deptno in (20, 30)
 order by sal + nvl(comm, 0), ename desc;

输出结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7Kn7Scef-1615986893291)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615979656899.png)]

空排序

  1. null在前面(first 变成last则null 在最后面)
select sal ,comm from emp order by comm  nulls first;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OMzOaMz7-1615986893296)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615980186444.png)]

函数

函数分为系统内置函数 自定义函数


  1. 单行函数:一条记录返回一个结果
  2. 多行函数 组函数 聚合函数 (重点) :多条记录 返回一个结果

单行函数

  1. 日期函数

  2. 日期函数: 注意区分 db数据库时间 ,java应用服务器的
    时间。以一方为准

    • oracle以内部数字格式存储日期年月日小时分钟秒

      select sysdate from dual;
      

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WoSRha2N-1615986893302)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615981024350.png)]

    • sysday /current_date以date 类型返回当前的日期

      select sysdate from dual;
      
    • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dqJBkWj9-1615986893304)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615981031268.png)]

    • add_months(d,x)返回加上x月后的日期d的值

      select add_months(sysdate,5)from dual ;
      

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OGFG219H-1615986893306)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615981231555.png)]

    • months_between(date1,date2) 返回date1和date2之间月的数目

      select ename,months_between(sysdate,hiredate) from emp;
      

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4qI2a2Sj-1615986893307)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615981455843.png)]

    • next_day(sysdate,星期一) 下周星期一(返回下一个周几)

      select next_day (sysdate ,'星期三')from dual;
      

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Sh0DThEO-1615986893309)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615981662312.png)]

转换函数

  1. to_date(c,m)->字符串指定形式转换成日期

    • yyyy代表年yer
    • mm代表月months
    • dd 代表天day
    • hh24代表24小时hh12 代表12小时
    • mi 代表分钟
    • ss代表秒
    select to_date ('1998-01-10 00:00:00','yyyy-mm-dd hh24:mi:ss')time from dual
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4ZpTr9FI-1615986893310)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615982260202.png)]

  2. to_char(d,m)日期指定格式转换为字符串

    select to_char(sysdate,'yyyy-mm-dd')from dual ;
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7T2ZTSZz-1615986893312)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615982748249.png)]

select *from emp where hiredate like '%82%';
--82年入职的员工信息
select *from emp where to_char(hiredate,'yyyy')='1982';

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uGUiYjQK-1615986893313)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615982929324.png)]

其他函数

  1. nvl(x,y)如果x=null输出y,否则输出x

    select (sal+nvl(comm,0))*12 年薪 from emp ;
    

    comm为null时显示为0,否则null与任何值相加都为null,无法计算年薪

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nJl7AYPZ-1615986893315)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615983483244.png)]

  • decode 函数

    书写格式

    decode(条件,值1,结果1,值2 ,
    结果2,….)

    select ename,decode(deptno, 10,'十',20,'二十')from emp;
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MDzmIRP6-1615986893316)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615983949861.png)]

select 
	ename,sal,deptno,
	decode(deptno,
		10,
		sal * 1.1,
		20,
		sal * 1.08,
		30,
		sal * 1.15,
		sal * 1.2) raisesal
from emp;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-afpM7FFx-1615986893317)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615984214965.png)]

  • case when then else end
select ename,
sal,
deptno,
(case deptno
when 10 then
sal * 1.1
when 20 then
sal * 1.08
when 30 then
sal * 1.15
else
sal * 1.2
end) raisesal
from emp;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ar3Ma55K-1615986893318)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615984401581.png)]

多行|聚合|组函数

avg 、sum、 min、max、count

  1. avg(平均值)

    select avg(sal)from emp ;
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rmNUkfCx-1615986893319)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615984693135.png)]

  2. sum(求和)

    select sum(sal) from emp;
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5S0V5KSr-1615986893321)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615984762612.png)]

  3. min(最小值)

select min (sal) from emp;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hrAn6L2w-1615986893322)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615984855598.png)]

  1. max(最大值)

    select max (sal)from emp ;
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LOyryk7s-1615986893323)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615984925332.png)]

  2. count(统计记录数)

    select count(comm)from emp;
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GKiduh0N-1615986893324)(C:\Users\MATEBOOK\AppData\Roaming\Typora\typora-user-images\1615985012065.png)]

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值