SQL-SELECT语句中的IF-有什么问题?

时间:2018-08-20 22:08:29

标签: mariadb

MariaDB error

BEGIN
    SELECT
       (SELECT CONCAT(lastname,' ',firstname) FROM actor WHERE actor.ID = studentminutes.STUDENT_ID),
       CASE (SELECT SUBSTR(ainscription.LEVEL_REPEAT, 7, 1) FROM ainscription WHERE ainscription.ID = studentminutes.AINSCRIPTION_ID)
            WHEN '1' THEN
                IF(sem1_ses1_credit = 30.0 OR sem1_ses2_credit = 0.0, sem1_ses1_credit, sem1_ses2_credit) + get_crt_sem(2, studentminutes.STUDENT_ID, studentminutes.AINSCRIPTION_ID) as crt
            WHEN '2' THEN
                IF(sem2_ses1_credit = 30.0 OR sem2_ses2_credit = 0.0, sem2_ses1_credit, sem2_ses2_credit) + get_crt_sem(1, studentminutes.STUDENT_ID, studentminutes.AINSCRIPTION_ID) as crt
            WHEN '3' THEN
                IF(sem1_ses1_credit = 30.0 OR sem1_ses2_credit = 0.0, sem1_ses1_credit, sem1_ses2_credit) + IF(sem2_ses1_credit = 30.0 OR sem2_ses2_credit = 0.0, sem2_ses1_credit, sem2_ses2_credit) as crt 
        END,
        CASE (SELECT SUBSTR(ainscription.LEVEL_REPEAT, 7, 1) FROM ainscription WHERE ainscription.ID = studentminutes.AINSCRIPTION_ID)
            WHEN '1' THEN
                (IF(sem1_ses1_credit = 30.0 OR sem1_ses2_credit = 0.0, sem1_ses1_pass_mark, sem1_ses2_pass_mark) + get_moy_sem(2, studentminutes.STUDENT_ID, studentminutes.AINSCRIPTION_ID))/2 as moy
            WHEN '2' THEN
                (IF(sem2_ses1_credit = 30.0 OR sem2_ses2_credit = 0.0, sem2_ses1_pass_mark, sem2_ses2_pass_mark) + get_moy_sem(1, studentminutes.STUDENT_ID, studentminutes.AINSCRIPTION_ID))/2 as moy
            WHEN '3' THEN
                (IF(sem1_ses1_credit = 30.0 OR sem1_ses2_credit = 0.0, sem1_ses1_pass_mark, sem1_ses2_pass_mark) + IF(sem2_ses1_credit = 30.0 OR sem2_ses2_credit = 0.0, sem2_ses1_pass_mark, sem2_ses2_pass_mark))/2 as moy    
        END
    FROM 
        minutes,
        minutes_studentminutes,
        studentminutes
    WHERE
        minutes_studentminutes.Minutes_ID = minutes.ID
        AND
        minutes_studentminutes.StudentsMinutes_ID = studentminutes.ID
        AND
        minutes.ACADEMICYEAR_ID = academicyearid 
        ORDER BY crt DESC, moy DESC;
END

1 个答案:

答案 0 :(得分:0)

如您的错误消息中所示,错误为“ as crt”。这是因为别名需要应用于CASE语句,而不是每个THEN语句。

因此,作为一个可行的案例:

SELECT 
    CASE '1'
    WHEN '1' THEN 'ONE' 
    WHEN '2' THEN 'TWO'
    END AS NUM
FROM dual;