--select case when 的使用
select
case
when 条件1 then action1
when 条件2 then action2
when 条件3 then action3
else actionN
end case
from table;
--
case selector
when value1 then action1
when value2 then action2
when value3 then action3
else actionN
end [case]
--使用case when判断是第几月份
select case when substr('20090310',5,2)='01' then '一月份'
when substr('20090310',5,2)='02' then '二月份'
when substr('20090310',5,2)='03' then '三月份'
when substr('20090310',5,2)='04' then '四月份'
else null
end
from dual;
--先建立一个成绩表
--把每个学生的grade列,用相应的等级来更新。
create table 成绩(sno number,km varchar2(10),score number,grade char(6));
insert into 成绩 values(1,'语文',65,null);
insert into 成绩 values(2,'数学',76,null);
insert into 成绩 values(3,'英语',86,null);
insert into 成绩 values(4,'语文',94,null);
commit;
--case等级,把学生分为优秀、良好、中等、及格。(case when第一种写法)
select
case
when score>=90 then '优秀'
when score>=80 then '良好'
when score>=70 then '中等'
when score>=60 then '及格'
else '不及格'
end grade
from 成绩;
--使用select case when 判断生成临时表a,然后update 成绩 set grade,然后再使用where条件引出grade
select
sno,
case
when score>=90 then '优秀'
when score>=80 then '良好'
when score>=70 then '中等'
when score>=60 then '及格'
else '不及格'
end grade
from 成绩;
--给原来的表增加等级信息
update 成绩 set grade=(select grade from(select
sno,
case
when score>=90 then '优秀'
when score>=80 then '良好'
when score>=70 then '中等'
when score>=60 then '及格'
else '不及格'
end grade
from 成绩) a where 成绩.sno=a.sno);
--查询成绩表的信息
select * from 成绩;
发现已经成功