case when then 的使用

case when then 结构有两种格式,分别是 确定条件列 和 不确定条件列。

case 列 

  when 值1 then 返回值1

  when 值2 then 返回值2

  else 默认返回值

end 列名

这种方式默认每个when 后面的条件是 ‘列=值’

case 

  when 条件1 then 返回值1

  when 条件2 then 返回值2

  else 默认返回值

end 列名

这种方式,条件 可以是该行记录任何情况的条件,只要返回布尔值就行。

select *,
    case score 
    when 100 then '满分'
    else score
  end '分数'
from stucores ;
select *,
    case  
    when score=100 then '满分'
    when score<100 and score>=60 then '及格'
    when score<60 then '不及格'
  end '评级'
from stucores ;

 

posted @ 2019-04-18 16:21  笔记Next  阅读(7176)  评论(0编辑  收藏  举报