• MySql 中 case when then else end 的用法


    便于理解,直接上例子:

    SELECT            
        case                   -------------如果
        when sex='1' then '男' -------------sex='1',则返回值'男'
        when sex='2' then '女' -------------sex='2',则返回值'女'  
        else 0                 -------------其他的返回'其他’
        end                    -------------结束
    from   sys_user            --------整体理解: 在sys_user表中如果sex='1',则返回值'男'如果sex='2',则返回值'女' 否则返回'其他’

    ---用法一:
    SELECT
                CASE WHEN STATE = '1' THEN '成功'
                     WHEN STATE = '2' THEN '失败'
                ELSE '其他' END  
                FROM  SYS_SCHEDULER
    ---用法二:    
    SELECT STATE
                CASE WHEN '1' THEN '成功'
                     WHEN '2' THEN '失败'
                ELSE '其他' END  
                FROM  SYS_SCHEDULER

    列子:

    有员工表empinfo
    (
    Fempno varchar2(10) not null pk,
    Fempname varchar2(20) not null,
    Fage number not null,
    Fsalary number not null
    );
    假如数据量很大约1000万条;写一个你认为最高效的SQL,用一个SQL计算以下四种人:
    fsalary>9999 and fage > 35
    fsalary>9999 and fage < 35
    fsalary <9999 and fage > 35
    fsalary <9999 and fage < 35
    每种员工的数量;
    select sum(case when fsalary > 9999 and fage > 35
    then 1
    else 0end) as "fsalary>9999_fage>35",
    sum(case when fsalary > 9999 and fage < 35
    then 1
    else 0
    end) as "fsalary>9999_fage<35",
    sum(case when fsalary < 9999 and fage > 35
    then 1
    else 0
    end) as "fsalary<9999_fage>35",
    sum(case when fsalary < 9999 and fage < 35
    then 1
    else 0
    end) as "fsalary<9999_fage<35"
    from empinfo;

  • 相关阅读:
    每日一题_191101
    阿基米德三角形(交互式学件)
    2018四川高考数学(全国卷3)理科21题以泰勒公式为命题背景(同时深挖去年高考题)和它的另类解法的瞎谈
    给老谢画的图(平面几何中的动点与最值问题)
    2018四川高考文科21题
    数学解题的思维过程
    Qt Creator 模块QtSql
    QT Creator快捷键不能用
    QT 随机数
    C++ 4 种具有更 为准确语义的新强制转换类型
  • 原文地址:https://www.cnblogs.com/weiyi1314/p/6489112.html
Copyright © 2020-2023  润新知