如何在存储过程WHERE子句中使用IF / CASE语句

时间:2017-08-18 07:09:36

标签: mysql database stored-procedures

我有这样的存储过程

DELIMITER $$

DROP PROCEDURE IF EXISTS `shashiTest`$$

CREATE DEFINER=`user`@`%` PROCEDURE `shashiTest`(IN GroupId INT, IN UserId INT, IN ChapterId INT)
BEGIN
    select 
    sum(qptdq.mark)-sum(qpe.marks) as marks_detucted,
    count(qpe.tc_question_message_type_id) AS noof_errors,
    (SELECT tqmt.key_value from tc_question_message_type tqmt WHERE tqmt.id=qpe.tc_question_message_type_id) AS error_type
    from question_paper qp
    INNER JOIN question_paper_group_student_map qpgsm ON qpgsm.question_paper_id=qp.id
    INNER JOIN institute_group_student_map igsm ON igsm.id=qpgsm.institute_group_student_map_id AND igsm.group_id=GroupId AND igsm.user_id=UserId
    INNER JOIN question_paper_template_detail qptd ON qptd.question_paper_template_id=qp.question_paper_template_id
    INNER JOIN question_paper_template_detail_question qptdq ON qptdq.question_paper_template_detail_id=qptd.id
    INNER JOIN question_paper_details qpd ON qpd.question_paper_template_detail_question_id=qptdq.id AND qpd.question_paper_id=qp.id
    INNER JOIN tc_question_mapping tcm ON tcm.tc_question_id=qpd.question_id
    INNER JOIN question_paper_evaluation qpe ON qpe.child_question_id=qptdq.id AND qpe.user_id=30 AND qpe.question_paper_id=qp.id AND qpe.tc_question_message_type_id IS NOT NULL
    INNER JOIN topic t ON t.cms_id=tcm.syllabus_chapter_details_id
    INNER JOIN chapter c ON c.id=t.chapter_id   
    IF (ChapterId IS NOT NULL)  THEN
        WHERE qp.question_paper_status_id >= 7 and c.id = ChapterId
    ELSE
        WHERE qp.question_paper_status_id >= 7
    END IF;
    GROUP BY qpe.tc_question_message_type_id LIMIT 5;
    END$$

DELIMITER ;

我想根据我的参数添加where条件,因为我有一个参数可选。 问题:我没有正确的语法。 我怎样才能做到这一点。请帮忙。感谢。

2 个答案:

答案 0 :(得分:0)

DELIMITER $$

DROP PROCEDURE IF EXISTS `shashiTest`$$

CREATE PROCEDURE `shashiTest`(IN GroupId INT, IN UserId INT, IN ChapterId INT)
BEGIN
    select 
    sum(qptdq.mark)-sum(qpe.marks) as marks_detucted,
    count(qpe.tc_question_message_type_id) AS noof_errors,
    (SELECT tqmt.key_value from tc_question_message_type tqmt WHERE tqmt.id=qpe.tc_question_message_type_id) AS error_type
    from question_paper qp
    INNER JOIN question_paper_group_student_map qpgsm ON qpgsm.question_paper_id=qp.id
    INNER JOIN institute_group_student_map igsm ON igsm.id=qpgsm.institute_group_student_map_id AND igsm.group_id=GroupId AND igsm.user_id=UserId
    INNER JOIN question_paper_template_detail qptd ON qptd.question_paper_template_id=qp.question_paper_template_id
    INNER JOIN question_paper_template_detail_question qptdq ON qptdq.question_paper_template_detail_id=qptd.id
    INNER JOIN question_paper_details qpd ON qpd.question_paper_template_detail_question_id=qptdq.id AND qpd.question_paper_id=qp.id
    INNER JOIN tc_question_mapping tcm ON tcm.tc_question_id=qpd.question_id
    INNER JOIN question_paper_evaluation qpe ON qpe.child_question_id=qptdq.id AND qpe.user_id=30 AND qpe.question_paper_id=qp.id AND qpe.tc_question_message_type_id IS NOT NULL
    INNER JOIN topic t ON t.cms_id=tcm.syllabus_chapter_details_id
    INNER JOIN chapter c ON c.id=t.chapter_id   
    WHERE
    CASE WHEN ChapterId IS NOT NULL THEN  //<---I had made changes at this line
         qp.question_paper_status_id >= 7 and c.id = ChapterId
    ELSE
       qp.question_paper_status_id >= 7
    END
    GROUP BY qpe.tc_question_message_type_id LIMIT 5;
    END$$

DELIMITER ;

您可以尝试以上代码。

我添加了CASE WHEN

答案 1 :(得分:0)

IF (ChapterId IS NOT NULL)  THEN
    WHERE qp.question_paper_status_id >= 7 and c.id = ChapterId
ELSE
    WHERE qp.question_paper_status_id >= 7
END IF;

您可以将其重写为

WHERE qp.question_paper_status_id >= 7
  AND (c.id = ChapterId OR ChapterId IS NULL)

此处不需要IF或CASE声明。