mysql - How to use IF/CASE statement in stored procedure WHERE clause -


i have stored procedure this

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) marks_detucted,     count(qpe.tc_question_message_type_id) noof_errors,     (select tqmt.key_value tc_question_message_type tqmt tqmt.id=qpe.tc_question_message_type_id) error_type     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 , igsm.group_id=groupid , 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 , 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 , qpe.user_id=30 , qpe.question_paper_id=qp.id , qpe.tc_question_message_type_id 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 not null)          qp.question_paper_status_id >= 7 , c.id = chapterid     else         qp.question_paper_status_id >= 7     end if;     group qpe.tc_question_message_type_id limit 5;     end$$  delimiter ; 

i want add condition according parameters because have 1 parameter optional. prob: dont have right syntax that. how can that. please help. thanks.

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) marks_detucted,     count(qpe.tc_question_message_type_id) noof_errors,     (select tqmt.key_value tc_question_message_type tqmt tqmt.id=qpe.tc_question_message_type_id) error_type     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 , igsm.group_id=groupid , 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 , 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 , qpe.user_id=30 , qpe.question_paper_id=qp.id , qpe.tc_question_message_type_id 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            case when chapterid not null  //<---i had made changes @ line          qp.question_paper_status_id >= 7 , c.id = chapterid     else        qp.question_paper_status_id >= 7     end     group qpe.tc_question_message_type_id limit 5;     end$$  delimiter ; 

you can try above code.

i had added case when.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -