关闭

case函数使用介绍

207人阅读 评论(1) 收藏 举报

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






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:47336次
    • 积分:2375
    • 等级:
    • 排名:第16188名
    • 原创:70篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1018条
    文章分类