(SQL)sql语句if,ifnull, case when语句多种用法 -

    xiaoxiao2022-07-13  169

    1.–case搜索函数

    case when case 列名 when 条件 then 结果 else 其它结果 end 别名 case f. source when '1' then '来自附近的人' when '2' then '来自搜索' when '3' then '来自扫一扫' when '4' then '来自名片分享' when '5' then '来自群聊添加' else '' end

    2.判断用法

    SELECT predetermine_id, (CASE(a.effective_time > NOW()) WHEN 0 THEN 0 ELSE 1 END) effectiveStatus FROM jq_predetermine a

    3.–简单case函数

    case sex when '1' then '男' when '2' then '女’ else '其他' end

    1. IFNULL函数

    IFNULL(expr1,expr2)

    如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值 注意:空值不判断

    2.if

    表达式:IF( expr1 , expr2 , expr3 )

    expr1条件,条件为true,则值是expr2 ,false,值就是expr3

    最新回复(0)