大纲


    首页 mysql基础教程[basic] 详情
    mysql逻辑函数

    总结了mysql常用的逻辑函数

    if(expr1,expr2,expr3)

    SELECT IF(TRUE,'A','B');    -- 输出结果:A
    SELECT IF(FALSE,'A','B');   -- 输出结果:B
    

    case when

    select *
        case 
        when prod_price < 5 then '低价格'
        when prod_price < 8 then '中价格'
        when prod_price < 10 then '高价格'
        else '超高价格'
      end as 价格分类
    from products
    

    IFNULL(expr1,expr2)

    如果expr1是空值,则返回expr2

    SELECT IFNULL(NULL,'B');    -- 输出结果:B
    SELECT IFNULL('HELLO','B'); -- 输出结果:HELLO
    

    nullif(expr1,expr2)

    如果expr1=expr2成立,那么返回值为null,否则返回值为expr1的值

    SELECT NULLIF('A','A');     -- 输出结果:null
    SELECT NULLIF('A','B');     -- 输出结果:A
    

    isnull(expr)

    如果expr的值为null,则返回1,如果expr1的值不为null,则返回0

    SELECT ISNULL(NULL);        -- 输出结果:1
    SELECT ISNULL('HELLO');     -- 输出结果:0
    
    评论
    您尚未登录,请 登录 后评论
    共 0 条评论 | 欢迎尬评