case函数使用介绍

1.创建测试表:

代码如下:

CREATE TABLE students (
  id               int PRIMARY KEY,
  first_name       char(20),
  last_name        char(20),
  major            char(30),
  current_credits  char(3),
  grade     char(2));

INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
  VALUES (1, 'Scott', 'Smith', 'Computer Science', 98,null);

INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
  VALUES (2, 'Margaret', 'Mason', 'History', 88,null);

INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
  VALUES (3, 'Joanne', 'Junebug', 'Computer Science', 75,null);

INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
  VALUES (4, 'Manish', 'Murgratroid', 'Economics', 66,null);

2.查看相应数据

代码如下:

select * from students

3.更新语句

代码如下:
update students
set grade = (
select grade from
(
select id,
case when current_credits > 90 then 'a'
     when current_credits > 80 then 'b'
     when current_credits > 70 then 'c'
else 'd' end grade
from students
) a
where a.id = students.id
)

4.更新后结果

代码如下:
select * from students






阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010104750/article/details/51549605
上一篇oracle trunc()函数的使用方法
下一篇oracle数据库序列的用法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭