如何在查询结果中新添加一个可变的字段。比如我有一个成绩表,里面字段有name,grade,
现在要查询结果出来90分以上为A等,70到80为B等,60到70为C等,60一下为D等。就是在
查询结果中新加一个等级的字段显示ABCD等。
case when 就可以吗
create procedure dbname.proc_getGrade
(stu_no varchar(20),cour_no varchar(10))
BEGIN
declare stu_grade float;
select grade into stu_grade from grade where student_no=stu_no and course_no=cour_no;
if stu_grade>=90 then
select stu_grade,'A';
elseif stu_grade=80 then
select stu_grade,'B';
elseif stu_grade=70 then
select stu_grade,'C';
elseif stu_grade70 and stu_grade>=60 then
select stu_grade,'D';
else
select stu_grade,'E';
end if;
END
注意:IF作为一条语句,在END IF后需要加上分号“;”以表示语句结束,其他语句如CASE、LOOP等也是相同的。
mysql if
您可能感兴趣的代码
mysql 的nullif和ifnull和isnull by YuChao
python 使用MySQLdb连接mysql by 玉开Sir
MySQL 存储过程中使用 WHILE 循环语句 by 山药
Java 调用 MySQL 存储过程并获得返回值 by init0
MySql while语句使用示例 by 甄码农
简单的mysql用户自定义函数示例 by 文丐
在mysql存储过程中执行动态sql的方法 by 0晓风残月0
mysql性能优化:配置文件my.cnf的优化 by 赵小全
MySQL 获取数据库实际的大小 by wubai
mysql master/slave 同步错误Query partially completed on the master (error on master: 1317) by 金背二郎
python通过Tkinter显示网络图片 by 阮小七
© 2015 内存溢出
复制代码
保存代码
select a.name , a.grad ,
(case when grad >=90 then 'A
when grad>=70 and grad <90 then 'B'
when grad >=60 and grad <70 then 'C'
when grad<60 then 'D')
from talbe_name a