case [表达式1] when [表达式2] then [表达式22]…when [表达式n] then [表达式nn] end
解释:根据表达式1的值跟表达式2…n进行比对,比对得上就输出表达式22…nn的值,比对不上就不输出。
案例:
select t.*,
(case t.name when '小明' then '小明明' when '小冰' then '小冰冰' when '阿珍' then 'az' end) 别名,
(case when t.score<60 then 'C' when t.score>=60 and t.score<90 then 'B' else 'A' end) 等级
from stu1 t;
输出结果: