case函数被习惯性的称为流程控制函数
其主要应用有以下两种

1、简单case函数

枚举这个字段(或者是该字段的函数)所有可能的值*

形式为

CASE  <col_name>
   WHEN <value1> THEN <result1>
   WHEN <value2> THEN <result2>
   ...
   ELSE <result>
END 

select  day
when  1  then  "星期一"
when  2  then  "星期二"
when  3  then  "星期三"
when  4  then  "星期四"
when  5  then  "星期五"
when  6  then  "星期六"
else  "星期天"
end 
 

2、case搜索函数

类似if判断,case搜索函数可以写判断,并且只会返回第一个符合条件的值,其他case被忽略

形式

CASE
    WHEN <条件1> THEN <结果1>
    WHEN <条件2> THEN <结果2>
    ...
    ELSE <结果>
END 

例:判断一个人的成绩是属于哪个等级,并输出该
注:及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

case 
when  score<60  then"不及格"
when  score <=70  then "及格"
when  score<=80   then "中等"
when  score<=90   then "优良"
else   "优秀"
end  

更多推荐

SQL语句case函数