case when [val1 ] then [res1] ... else [default] end
如果 val1 为 true,返回 res1,… 否则返回 default 默认值 (范围)
case[ expr ] when [val1 ] then [res1] ... else [default] end
如果 expr 的值等于 val1,返回 res1,… 否则返回 default 默认值
create table score(
id int comment 'ID',
name varchar(20) comment '姓名',
math int comment '数学',
english int comment '英语',
chinese int comment '语文'
)comment '学生成绩表';
insert into score values (1,'Tom',67,88,95),(2,'Rose',23,66,90),
(3,'Jack',56,98,76);
select
id,
name,
(case when math >= 85 then '优秀' when math >=60 then '及格' else '不及格' end)'数学',
(case when english>= 85 then '优秀' when english >=60 then '及格' else '不及格' end)'英语',
(case when chinese >= 85 then '优秀' when chinese >=60 then '及格' else '不及格' end)'语文'
from score;