09.Oracle 高级查询-分支函数语句

Oracle 高级查询-分支函数/语句

nvl

nvl(列, 值)

如果查询列的值为null,那么显示为给定的值,如果不为nul,就显示为列原值

注意:null值不参与±*/以及聚合运算!

select sal, comm, sal + nvl(comm, 0) from emp;
nvl2

nvl2(列, 值1, 值2)

如果查询列的值为null,那么显示为给定值2, 如果不为null,则显示为给定值1

-- 如果雇员没有绩效,每人发100元,如果有绩效,每人再多发200元
select comm, nvl2(comm, comm + 200, 100) from emp;
decode

decode(列, 原值1, 给定值1, 原值2, 给定值2, …, [默认值])

如果不给默认值,最后返回null

相当于Java/JS中的switch等值比较!

select empno, ename, deptno, decode(deptno, 10, 'Manager', 20, 'Develop', 30, 'Sales', 'UNKNOWN') dname from emp;
case … end

它的作用也类似于Java/JS中的switch

但是它除了可以进行等值比较(完全等效于decode),还可以进行表达式(and or betwen in)比较

  1. 等值比较语法

case 列

​ when 值1 then 给定值1

​ when 值2 then 给定值2

​ when 值3 then 给定值3

​ else 默认值 – 如果不给默认值,最后返回null

end

select empno, ename,
    case deptno
        when 10 then 'Manager'
        when 20 then 'Develop'
        when 30 then 'Sales'
        -- else 'Unknown' 
    end dname
        from emp;
  1. 表达式比较语法

case

​ when 列表示式1 then 给定值1

​ when 列表示式2 then 给定值2

​ when 列表示式3 then 给定值3

​ else 默认值

end

select empno, ename, sal, nvl(comm, 0) comm,
    case
        when sal + nvl(comm, 0) between 0 and 999 then '低薪'
        when sal + nvl(comm, 0) between 1000 and 1999 then '低薪'
        when sal + nvl(comm, 0) between 2000 and 2999 then '中薪'
        when sal + nvl(comm, 0) between 3000 and 3999 then '高薪'
        else '最高'
    end grade
        from emp;

注意:所有的函数/case…end除了用于查询列,也可以用于更新值!

update emp
    set comm = nvl2(comm, comm + 200, 100);
-- 给不同部门的管理员岗位进行加薪(decode写法)
update emp
    set sal = decode(deptno, 10, sal + 0.01, 20, sal + 0.02, 30, sal + 0.03, sal)
        where job = 'MANAGER';
-- 给不同部门的管理员岗位进行加薪(case..end写法)
update emp
    set sal = 
        case deptno
            when 10 then sal + 0.01
            when 20 then sal + 0.02
            when 30 then sal + 0.03
            else sal
        end
            where job = 'MANAGER';

请实现:根据雇员的薪水范围加薪

0~999, + 10%

1000~1999, + 5%

2000~2999, + 2%

3000~3999, + 1%

4000+, 不加薪

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值