使用Case语句创建存储过程

时间:2016-07-21 17:55:20

标签: mysql sql stored-procedures

我正在尝试创建一个存储过程,我似乎弄错了。我只在需要时使用SQL,所以请原谅一个开发者并帮忙。我想在存储过程中包含一个SQL查询,该查询在两个表之间进行内部连接:user_students和student_grades_summary。这是我写的存储过程:

CREATE PROCEDURE GradeStudents( in scoreover100 float, out s_grade char(3))
BEGIN 
CASE
        WHEN (student_grades_summary.aggregate >= 80 && student_grades_summary.aggregate <= 100 && student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'A1';
        WHEN(student_grades_summary.aggregate >= 70 AND student_grades_summary.aggregate <= 79 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'B1';
        WHEN (student_grades_summary.aggregate >= 60 AND student_grades_summary.aggregate <= 69 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'B2';
        WHEN(student_grades_summary.aggregate >= 55 AND student_grades_summary.aggregate <= 59 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'P1';
        WHEN(student_grades_summary.aggregate >= 50 AND student_grades_summary.aggregate <= 54 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'P2';
        WHEN(student_grades_summary.aggregate <= 49 AND student_grades_summary.level = 'JSS')
            SET student_grades_summary.grade = 'F';
        WHEN(student_grades_summary.aggregate >= 85 AND student_grades_summary.aggregate <= 100 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'A1';
        WHEN(student_grades_summary.aggregate >= 80 AND student_grades_summary.aggregate <= 84 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'B2';
        WHEN(student_grades_summary.aggregate >= 75 AND student_grades_summary.aggregate <= 79 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'B3';
        WHEN(student_grades_summary.aggregate >= 70 AND student_grades_summary.aggregate <= 74 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'C4';
        WHEN(student_grades_summary.aggregate >= 65 AND student_grades_summary.aggregate <= 69 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'C5';
        WHEN(student_grades_summary.aggregate >= 60 AND student_grades_summary.aggregate <= 64 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'C6';
        WHEN(student_grades_summary.aggregate >= 55 AND student_grades_summary.aggregate <= 59 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'D7';
        WHEN(student_grades_summary.aggregate >= 50 AND student_grades_summary.aggregate <= 54 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'E8';
        WHEN(student_grades_summary.aggregate <= 49 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'F9';
END CASE;

这是我试图包含存储过程的SQL查询。基本上我试图通过使用存储过程将值添加到student_grades_summary中的grade列。无论如何这里是sql:

select user_students.fname as FirstName, user_students.lname as LastName
     , student_grades_summary.subject as CoreSubjects
     , student_grades_summary.aggregate as Scoreover100
     , student_grades_summary.subjectrank as Position
     , student_grades_summary.term as Term
     , student_grades_summary.level as Level
     , student_grades_summary.class as Class
     , student_grades_summary.section as Section
     , student_grades_summary.session as Session
FROM student_grades_summary 
JOIN user_students 
   ON student_grades_summary.level = user_students.level 
   AND student_grades_summary.class = user_students.class 
   AND student_grades_summary.section = user_students.section 
JOIN config_grades 
   ON student_grades_summary.level = config_grades.level 
WHERE user_students.level = 'JSS' 
   AND user_students.class = '2' 
   AND student_grades_summary.session = '2015/2016'

所以在phpmyadmin中,我得到了很多红色波浪线,说SQL中有错误。我试图查看代码并找出错误但却无法解决的问题。

1 个答案:

答案 0 :(得分:0)

我一次又一次地看到。你没有在SQL中思考。在SQL中,您需要在集合中思考。不要想在子程序中进行思考。在这种情况下,您的集合是成绩标准的列表,我们可以将其表示为一个表格(我称之为grade_lookup)

aggmin aggmax lvl   grade
80       100  'JSS' 'A1'      
70       79   'JSS' 'B1'
60       69   'JSS' 'B2'
55       59   'JSS' 'P1'
50       54   'JSS' 'P2'
0        49   'JSS' 'F' 
85       100  'SSS' 'A1' 
80       84   'SSS' 'B2'
75       79   'SSS' 'B3'
70       74   'SSS' 'C4'
65       69   'SSS' 'C5'
60       64   'SSS' 'C6'
55       59   'SSS' 'D7'
50       54   'SSS' 'E8'
0        49   'SSS' 'F9'

一旦你有了grade_lookup表,你加入它并获得你的成绩 - 不需要函数调用 - 像这样:

select user_students.fname as FirstName, user_students.lname as LastName
     , s.subject as CoreSubjects
     , s.aggregate as Scoreover100
     , s.subjectrank as Position
     , s.term as Term
     , s.level as Level
     , s.class as Class
     , s.section as Section
     , s.session as Session

     -- the result of our lookup
     , COALESCE(L.grade ,'lookup fail') as Grade

FROM student_grades_summary S
JOIN user_students 
   ON student_grades_summary.level = user_students.level 
   AND student_grades_summary.class = user_students.class 
   AND student_grades_summary.section = user_students.section 
JOIN config_grades 
   ON student_grades_summary.level = config_grades.level 

-- join to grade lookup
LEFT JOIN GRADE_LOOKUP L ON s.aggregate >= L.aggmin 
                   and s.aggregate <= L.aggmax 
                   and s.level = L.lvl

WHERE user_students.level = 'JSS' 
   AND user_students.class = '2' 
   AND student_grades_summary.session = '2015/2016'
  

请注意,我只是将其粘贴到您的选择语句中 - 我不知道您的选择语句是否正确 - 请勿考虑重新使用您的示例代码作为验证#&# 39;正确性。