0

试图开发一个系统来为学生分配成绩。系统从我的数据库中读取分数,并在获得平均分数后分配一个等级。使用 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;



  //
4

1 回答 1

0

首先,您只想优化您的功能

(我不是您的语言专家(我认为是 Vb 对吗?)):

set phyz = (select Physics from marks where Sid=v_idnumber);
set Chem = (select Chemistry from marks where Sid=v_idnumber);

if(phyz>Chem)
    set v_marks=(select(English+Kisw+Math+Physics+Bio+Business+Geog+Cre)/8 from marks where Sid=v_idnumber);
else /*if (Chem > phyz) I comment the "if" for treatment where chem > phyz and Chem == phyz */
    set v_marks=(select(English+Kisw+Math+Chemistry+Bio+Business+Geog+Cre)/8 from marks where Sid=v_idnumber);
end if

/* You have just one same treatment for all types (phyz > chem ; chem >phyz, etc...) 
   When you optimize you code, verify your code isn't duplicate :-) 
*/ 

if (v_marks>= 80 && v_marks<=100)
    set grade = 'A'; 
else if (v_marks>=60 && v_marks<=79) 
    set grade = 'B';
else if (v_marks>=40 && v_marks<=59)
    set grade = 'C';
else if (v_marks>=30 && v_marks<=49)
    set grade = 'D';
else if (v_marks>=0 && v_marks<=29)
    set grade = 'E';  
else
    set grade = 'Failed Miserably';
end if

return grade;
于 2012-08-07T22:49:18.170 回答