流程函数也是很常用的一类函数,可以在SQL语句中实现条件筛选,从而提高语句的效率
课堂练习:
1、如果是true返回’成功’,false返回’失败’
2、如果是空返回’空’,否则返回’不空’
3、查询emp表的员工姓名和工作地址(蜀国——>四川地区,魏国—->中原,吴国——>鱼米之乡)
select name,
(case workaddress when ‘魏国’ then ‘四川地区’ when ‘蜀国’ then ‘中原地区’ when ‘吴国’ then ‘鱼米之乡’ end) as ‘工作地址’
from emp
案例 • 根据需求完成以下SQL编写
统计班级各个学员的成绩,展示的规则如下:
• >= 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(id, name, math, english, chinese) 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;