试图开发一个系统来为学生分配成绩。系统从我的数据库中读取分数,并在获得平均分数后分配一个等级。使用 case 语句,不断抛出错误。所以我尝试了 if 语句,但它给了我问题。有人知道更好的方法吗?
delimiter //
create function assign_grade(v_idnumber int)
returns varchar(50) deterministic
begin
declare v_marks1 varchar(50);
declare v_marks2 varchar(20);
declare phyz varchar(50);
declare Chem varchar(50);
declare grade varchar(50);
set phyz = (select Physics from marks where Sid=v_idnumber);
set Chem=(select Chemistry from marks where Sid=v_idnumber);
if(phyz>Chem)
begin
set v_marks1=(select(English+Kisw+Math+Physics+Bio+Business+Geog+Cre)/8 from marks where Sid=v_idnumber);
if (v_marks1>= 80 && v_marks1<=100) then
set grade ='A';
else if (v_marks1>=60&& v_marks1<=79)
set grade='B';
else if (v_marks1>=40&& v_marks1<=59)
set grade ='C';
else if (v_marks1>=30&& v_marks1<=49)
set grade ='D';
else if (v_marks1>=0&& v_marks1<=29)
set grade ='E';
else
set grade = 'Failed Miserably';
end if
end if
end if
end if
end if
return grade;
end;
if(Chem>phyz)
begin
set v_marks2=(select(English+Kisw+Math+Chemistry+Bio+Business+Geog+Cre)/8 from marks where Sid=v_idnumber);
if (v_marks2>= 80 && v_marks2<=100) then
set grade ='A';
else if (v_marks2>=60&& v_marks2<=79)
set grade='B';
else if (v_marks2=40&& v_marks2<=59)
set grade ='C';
else if (v_marks2>=30&& v_marks2<=49)
set grade ='D';
else if (v_marks2>=0&& v_marks2<=29)
set grade ='E';
else
set grade = 'Failed Miserably';
end if
end if
end if
end if
end if
end;
return grade;
//