SQL-基础流程函数-成绩表(datagrip开发工具)

流程函数

  1. if(value ,t, f) 如果value为true,则返回t,否则返回f;
  2. ifnull(value1, value2) 如果value1不为空,返回value1,否则返回value2;
  3. case when [val1 ] then [res1 ].....else[ default ] end 如果val1为true, 返回res1,.....否则返回default默认值;
  4. case [expr] when [ val1] then [res1] .....else[ default ] end 如果expr的值等于val1, 返回res1 ,.....否则返回default默认值;

实现简单需求:学生成绩等级划分

一.简单创建学生成绩表

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, '自律', 63, 90, 26 ), 
                                                           (2, '勤奋', 75, 87, 34),
                                                           (3, '上进', 89, 97, 100);

三.添加条件(大于80分优秀,大于60分及格,其他不及格)

select
     id,
     name,
    (case when math >= 80 then '优秀' when math  >=60 then '及格' else '不及格' end ) '数学',
    (case when english >= 80 then '优秀' when english >=60 then '及格' else '不及格' end ) '英语',
    (case when chinese >= 80 then '优秀' when  chinese >=60 then '及格' else '不及格' end ) '语文'
from score;

你可能感兴趣的:(sql,数据库,database)