MYSQL:Orderby Rand(),NOT IN子句不起作用

时间:2017-06-21 11:16:35

标签: mysql stored-procedures

进度:目前我正在mysql中编写一个存储过程。问题与marksquestion_type绑定。问题将在循环中获取取决于question_typemarks

问题:如果question_type和标记组合不匹配,那么它将使用标记组合获取随机问题。第一次工作,但之后不工作。

请检查下面给出的代码。

DELIMITER $$

DROP PROCEDURE IF EXISTS `generateQuestionPaper`$$

CREATE PROCEDURE `generateQuestionPaper`(IN questionPaperId INT, IN user_id INT)
BEGIN
    DECLARE chapterIds varchar(100);
    DECLARE templateId varchar(100);
    DECLARE done INT DEFAULT FALSE;
    DECLARE i INT DEFAULT 0;
    DECLARE selectedQuestionNumber INT DEFAULT 0;
    DECLARE curId INT DEFAULT 0;
    DECLARE curMark INT DEFAULT 0;
    DECLARE curQuestionType INT DEFAULT 0;
    DECLARE questionSelectionLoop CURSOR FOR SELECT tqb.id,tqb.mark,tqb.question_type FROM QBDetails tqb;   
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;
    DROP TEMPORARY TABLE IF EXISTS QBDetails;
    CREATE TEMPORARY TABLE QBDetails (id INT(11),mark INT(11),question_type INT(11),question_id INT(11) DEFAULT 0); 

    SELECT question_paper_template_id INTO templateId FROM question_paper WHERE id = questionPaperId; 

    SELECT GROUP_CONCAT(chapter_id) INTO chapterIds FROM question_paper_chapter_map WHERE question_paper_id =questionPaperId;

        INSERT INTO QBDetails (id,mark,question_type) SELECT qdq.id,qdq.mark,qdq.question_type_master_id FROM question_paper_template_detail_question qdq INNER JOIN question_paper_template_detail qpt ON qpt.question_paper_template_id = templateId GROUP BY qdq.id;

    START TRANSACTION;
        SET done = 0;
        OPEN questionSelectionLoop;
        get_question:LOOP
            FETCH questionSelectionLoop INTO curId,curMark,curQuestionType;
            IF done = 1 THEN 
                LEAVE get_question;
            END IF;
            BEGIN
                SELECT tqm.id INTO selectedQuestionNumber FROM tc_question_master tqm WHERE (tqm.marks = curMark AND tqm.question_type = curQuestionType) AND (tqm.id NOT IN (SELECT q.question_id FROM QBDetails q))  ORDER BY rand() LIMIT 1;
                -- From here my code breaks START
                IF selectedQuestionNumber=0 THEN
                    SELECT tqm.id INTO selectedQuestionNumber FROM tc_question_master tqm WHERE (tqm.marks = curMark) AND (tqm.id NOT IN (SELECT q.question_id FROM QBDetails q))  ORDER BY rand() LIMIT 1;
                END IF;
                UPDATE QBDetails qx SET qx.question_id = selectedQuestionNumber WHERE qx.id = curId;
                -- From here my code breaks END
                SET selectedQuestionNumber = 0;

            END;
        END LOOP get_question;
        CLOSE questionSelectionLoop;
    COMMIT;
    SELECT * from QBDetails;
    DROP TEMPORARY TABLE IF EXISTS QBDetails;
    END$$

DELIMITER ;

输出:

id  marks question_type question_id
5   2     210           991402
6   2     210           991405
7   2     210           991401
8   2     210           991411
9   2     252           1003386
10  2     253           1003388
11  2     253           1003384
13  1     1             933612
14  1     244           992444
15  3     211           991434
16  3     219           991374
17  3     256           1003382
18  3     219           1003379
19  3     211           0
20  3     256           0
21  4     242           0
22  5     213           0
24  1     248           0
25  1     245           0
27  1     244           0
28  1     224           0
29  1     215           0
30  1     206           0

请帮帮我。

0 个答案:

没有答案