mysql 数据库查询语句中关于case when then end 和 if/ifnull/nullif

  • case when then end 的使用
select case COLUMN
    when CONDITION1 then VALUE1
    when CONDITION2 then VALUE2
    else VALUE3
end
from table
// for example
select (case status
    when 'paying' then '支付中'
    when 'success' then '支付成功'
    when 'close' then '交易关闭'
    else '订单状态异常'
    end) AS orderStatus
from order

或者:

select case
    when COLUMN CONDITION1 then VALUE1
    when COLUMN CONDITION2 then VALUE2
    else VALUE3
    end
from table
// for example
select (case
    when status = 'paying' then '支付中'
    when status = 'success' then '付款成功'
    when status = 'close' then '交易关闭'
    else '订单状态异常'
    end) AS orderStatus
from order
-------
select (case
    when score >= 85 then '优秀'
    when score >= 70 and score < 85 then '良好'
    when score >= 60 and score < 70 then '及格'
    else '不及格'
    end) AS scoreLevel
from performance
  • if 的使用
select if(condition, value1, value2) from table
// 如果condition成立,那么值为value1,否则为value2,for example
select if(sex = 1, 'male', 'female') AS sex from user
  • ifnull 的使用
select ifnull(column, value) from table
// 如果columnnull,那么值为valuefor example
select ifnull(email, '不详') from user
  • nullif 的使用
select nullif(column1, column2) from table
// 如果column1的值等于column2的值,那么为null,否则为column1的值,for example
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值