/*流程控制函数*/
-- if
select if(true,'OK','Error');
-- ifnull
select ifnull('OK','Default');
select ifnull('','Default');/*返回第一个值*/
select ifnull(null,'Default');
-- case when then else and
-- 需求:查询emp表的员工姓名和工作地址(北京/上海---->一线城市,其他---->二线城市)
select name,case workadress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end from emp;
-- 案例:统计班级各个学员成绩,展示规则如下
-- >=85 优秀
-- >=60 及格
-- 否则 不及格
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 chinese >= 85 then '优秀' when chinese>=60 then '及格' else '不及格' end),
(case when english >= 85 then '优秀' when english>=60 then '及格' else '不及格' end)
from score;