Devinwon / article

0 stars 0 forks source link

存储过程 游标 触发器 #27

Open Devinwon opened 6 years ago

Devinwon commented 6 years ago

获取值时没有成功,提示 USERID,QUESTIONID Null `begin DECLARE done BOOLEAN DEFAULT 0 ; DECLARE QSID varchar(100) ; DECLARE USERID varchar(100) ; DECLARE QUESTIONS int ; DECLARE QUESTIONSyes int ; DECLARE QUESTIONSmark int ; DECLARE rel int DEFAULT 0 ;

DECLARE userid CURSOR FOR select userid from quiz_answeredhistory order by answeredDatetime desc limit 1 ;
DECLARE userQuestionMatrx CURSOR FOR select distinct questionid from quiz_answeredhistory where userid=(select userid from quiz_answeredhistory order by answeredDatetime desc limit 1);

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
OPEN userQuestionMatrx;
REPEAT
    FETCH userQuestionMatrx  INTO QSID;
    select userid from quiz_answeredhistory order by answeredDatetime desc limit 1 into USERID;
    select count(*) from quiz_answeredhistory where questionid=QSID and userid=USERID  into QUESTIONS ;
    select count(*) from quiz_answeredhistory where questionid=QSID and answeredRW=1  and userid=USERID into QUESTIONSyes ;
    select mark from quiz_question where id=QSID into QUESTIONSmark;
    select count(*) from quiz_userquestionmark where  questionid=QSID and userid=USERID  into rel  ;
   if  rel =0 then 
         insert into quiz_userquestionmark values('1',USERID,QSID, QUESTIONSmark*QUESTIONSyes/QUESTIONS);
  end if;
UNTIL done END REPEAT;
CLOSE userQuestionMatrx;

end`