在MYSQL存储过程中更新查询

时间:2016-08-05 08:27:13

标签: mysql mysql-workbench

当我在mysql workbench中执行以下更新查询时,它只会更新2行。

UPDATE usertoken 
  SET StatusName='Completed',Id=null  
  WHERE (UserId=45 and BuddyId=46) OR (UserId=46 and BuddyId=45);

但是存储过程中的相同查询会更新整个表。

即使我在存储过程中硬编码了用户ID,也没有改变。

任何想法?

更新:

CREATE DEFINER=`root`@`%` PROCEDURE `SubmitResult`(IN userId int,IN buddyID int,IN startTime DATETIME,IN endtime DATETIME,IN questions varchar(255),IN answers varchar(255))
BEGIN
SET @userId = userId;
SET @questions = questions;
SET @answers = answers;
SET @buddyID = buddyID;


SELECT @QuizId := QuizId from usertoken where (UserId=@userId and BuddyId=@buddyID) OR (UserId=@buddyID and BuddyId=@userId) LIMIT 1;

IF @QuizId IS NULL THEN 

    INSERT INTO QUIZ (QuizStartTime, QuizEndTime, QuizTypeId)
    SELECT startTime,endtime,1;

    SELECT @newQId := MAX(QuizId) from QUIZ LIMIT 1;

    SELECT @userId,@buddyID,@newQId;

    /* UPDATE usertoken SET QuizStatusName='Completed',QuizId=@newQId  where (UserId=@userId and BuddyId=@buddyID) OR (UserId=@buddyID and BuddyId=@userId); */
    UPDATE usertoken SET QuizStatusName='Completed',QuizId=@newQId  where UserId=45; 
    UPDATE usertoken SET QuizStatusName='Completed',QuizId=@newQId  where UserId=46;

// Some Statements

END IF;

SET @userId = NULL;
SET @questions = NULL;
SET @answers = NULL;
SET @buddyID = NULL;
SET @startTime = NULL;
SET @endtime = NULL;
SET @QID = NULL;
SET @AID = NULL;
SET @n = NULL;
SET @newQMId = NULL;
SET @newQId = NULL;
SET @QuizId = NULL;

END

1 个答案:

答案 0 :(得分:1)

我的更新查询在使用表别名前缀where条件之后运行良好。不确定它为什么失败但我认为存储过程IN参数具有相同的userId属性并且可能是原因..

为我的更新查询添加前缀

UPDATE usertoken t   SET t.StatusName ='已完成',t.Id = null
  WHERE(t.UserId = 45 and t.BuddyId = 46)OR(t.UserId = 46 and t.BuddyId = 45);