zoukankan      html  css  js  c++  java
  • mysql之case..when ..then..else..end as..用法

    1.示例1

    查询1:作为查询结果

    select case when mdjlx = 'db_jz' then 1 when mdjlx = 'kn_jz' then 2 when mdjlx = 'wb_jz' then 3 else 4 end as mdjlx
    from sr_main_ww where mhzsfz = '330127199210021719';

    SELECT
    count(1) AS ncount,
    (
    CASE sys_sjbj
    WHEN 0 THEN
    '个人发布'
    WHEN 1 THEN
    '组织发布'
    WHEN 2 THEN
    '机构发布'
    ELSE
    '其他'
    END
    ) AS fblx,
    ifnull(
    sum(CASE rlzt WHEN 2 THEN 1 ELSE 0 END),
    0
    ) AS xyl,
    ifnull(
    ROUND(
    sum(
    TIMESTAMPDIFF(DAY, fbsj, rlsj)
    ) / sum(CASE rlzt WHEN 2 THEN 1 ELSE 0 END),
    1
    ),
    0
    ) AS pjrlsj
    FROM
    sr_xqfb
    WHERE
    fbrq LIKE '2019%'
    GROUP BY
    -- sys_sjbj
    fblx

    2.示例2

    更新表1:

    //更新表,将中文转为对应数字
    update st_qlsx set qlsx_lx = CASE WHEN qlsx_lx='许可' THEN '01' WHEN qlsx_lx='处罚' THEN '03'
    END ;

    3.示例3

    插入表1

    insert into sr_main_sjzhk(sys_spzt,sys_djzt,sys_scbj,mdjlx,pk_sr_main,sys_xzqh,xzqh,mhz,mhzsfz,mjtrk,main_xm_sam20,mjzksrq,main_xm_sam21,MAIN_XM_SAM22,MAIN_XM_SAM23)
    select (case when bzzt = '在保' then 1 else 4 end ) as sys_spzt, (case when bzzt = '在保' then 1 else 0 end ) as sys_djzt, 0 as sys_scbj,'ywgl_sjst' as mdjlx, ? as pk_sr_main,xzqh as sys_xzqh,xzqh as xzqh,bzdxxm as mhz,bzdxsfzh as mhzsfz,jtrs as mjtzrk,bzfs as main_xm_sam20,gxsj as mjzksrq,bzzt as main_xm_sam21 ,bzjtbh as MAIN_XM_SAM22 ,? as MAIN_XM_SAM23 from sr_gzf where SFZSQR = '是' and clbj =0 and id = ? and pk_sr_gzf = ?";

    4.作为where后的查询条件

    SELECT
    *
    FROM
    sr_main_ylyj a,
    mz_xzjg b
    WHERE
    a.sys_xzqh = b.xzqh_id
    AND b.node_id LIKE '33%'
    AND sys_scbj = 0
    AND a.sys_spzt = 0
    AND (
    CASE
    WHEN a.msfczdx IS NOT NULL
    AND a.msfczdx <> '' THEN
    0
    ELSE
    1
    END
    ) = '0'
    ORDER BY
    a.pk_sr_main_ylyj DESC
    LIMIT 0,
    15

  • 相关阅读:
    RocketMQ源码 — 十、 RocketMQ顺序消息
    RocketMQ源码 — 九、 RocketMQ延时消息
    RocketMQ源码 — 八、 RocketMQ消息重试
    HDU3439 Sequence
    Cipolla算法学习小记
    BZOJ2286: [Sdoi2011]消耗战
    BZOJ4873 寿司餐厅
    BZOJ1718 [Usaco2006 Jan] Redundant Paths 分离的路径
    BZOJ1123 [POI2008]BLO
    BZOJ3996 TJOI2015线性代数
  • 原文地址:https://www.cnblogs.com/tongcc/p/11390601.html
Copyright © 2011-2022 走看看