oracle菜鸟学习之 select case when的使用
admin
2023-04-16 07:41:41
0次

[toc]

oracle菜鸟学习之 select case when的使用

格式语法

case
    when 条件1 then action1
    when 条件2 then action2
    when 条件3 then action3
    when 条件N then actionN
    else action
end

例子

判断现在是几月

SQL> select case substr('20181118',5,2)
  2  when '08' then '8yue'
  3  when '09' then '9yue'
  4  when '10' then '10yue'
  5  when '11' then '11yue'
  6  when '12' then '12yue'
  7  else 'other'
  8  end
  9  from dual;

CASESUBSTR('201
---------------
11yue

SQL> 

扩展知识:substr 截取
sbustr('str',x,y)
str:字符串
x:从x位开始
y:x位的后y位结束

SQL> select substr('123456',3,2) from dual;

SUBSTR
------
34

SQL> 

实验

实验表如下:
sno:学号
km:科目
score:成绩
grade:等级

create table score(sno number,km varchar2(8),score int,grade varchar2(4) default null);
insert into score(sno,km,score) values(1,'yw',65);
insert into score(sno,km,score) values(2,'sx',76);
insert into score(sno,km,score) values(3,'yw',86);
insert into score(sno,km,score) values(4,'yw',94);

查看表

SQL> select * from score;

       SNO KM                SCORE GRADE
---------- ------------------------ ---------- ------------
     1 yw                   65
     2 sx                   76
     3 yw                   86
     4 yw                   94

问题:给学生成绩分等级,优秀、良好、中等、合格
思路:先查询学号对应的成绩

SQL> select sno,case
  2  when score >=90 then 'A'
  3  when score >=80 then 'B'
  4  when score >=70 then 'C'
  5  when score >=60 then 'D'
  6  else 'F'
  7  end
  8  from score;

       SNO CAS
---------- ---
     1 D
     2 C
     3 B
     4 A

思路:怎么将等级插入表格?

update score set grade = ?

思路:选出等级的值

select grade from
(select sno,case
 when score >=90 then 'A'
 when score >=80 then 'B'
 when score >=70 then 'C'
 when score >=60 then 'D'
 else 'F'
 end as grade
  9   from score);

GRADE
----------
D
C
B
A

思路:grade不能等于一个集合,只能等于某个值,怎么选出某个值?
oracle菜鸟学习之 select case when的使用

从图中可以看出,如果我把第一个表取别名为a,但a.sno和score.sno相等的时候,grade的值唯一

update score set grade = 
(select grade from
(select sno,case
 when score >=90 then 'A'
 when score >=80 then 'B'
 when score >=70 then 'C'
 when score >=60 then 'D'
 else 'F'
 end as grade
 from score) a
 where a.sno=score.sno
 );

4 rows updated.

查看更新之后的表

SQL> select * from score;

       SNO KM                SCORE GRADE
---------- ------------------------ ---------- ----------
     1 yw                   65 D
     2 sx                   76 C
     3 yw                   86 B
     4 yw                   94 A

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...