当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql中的case语句和if()实例

mysql中的case语句和if()实例

2018年09月25日  | 移动技术网IT编程  | 我要评论

当逻辑结构比较繁琐时,用if()嵌套结构比较麻烦,可以使用case语句。

--格式
case when <求值表达式> then <表达式>
     when <求值表达式> then <表达式>
     when <求值表达式> then <表达式>
       .
       .
       .
     else <表达式>
end

比如对每个学生成绩进行分类,可以这样写:

select 
        name, score, 
        (case 
        when score between 0 and 59.99 then '不及格'
        when score between 60 and 69.99 then '一般般'
        when score between 70 and 79.99 then '良'
        when score between 80 and 99.99 then '优秀'
        else '陈独秀' end
        ) as 'scroe_group'
    from
        student_table

当然也可以用if()来实现,但是这没case语句简单明了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网