我正在尝试在更新语句中添加一个 IF 子句。以为这很容易,但似乎并非如此。
事情是这样的。它在存储过程中。
FETCH cur1 INTO procId, procType, procVals, procLen, procUpdated, procPrivate, procRegional;
IF done THEN
LEAVE the_loop;
END IF;
UPDATE scores t1
JOIN scores t2
ON FIND_IN_SET(t1.id, t2.vals)
SET t1.private = t1.private+1,
IF procType = 3 THEN // Problem lies here
t1.regional = t1.regional+1;
ELSE IF procType = 4 THEN
t1.otherCol = t1.otherCol+1;
END IF;
WHERE t2.id = procId;
我被那里的 IF
困住了。除了第一个 SET
,我还需要使用 IF
Update
另一列。
最佳答案
UPDATE scores t1
JOIN scores t2
ON FIND_IN_SET(t1.id, t2.vals)
SET t1.private = t1.private+1
t1.regional = IF (procType = 3, t1.regional + 1, t1.regional)
t1.otherCol = IF (procType = 4, t1.otherCol + 1, t1.otherCol)
WHERE t2.id = procId
关于mysql - 在更新语句中添加 If,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110417/