0

我正在尝试在更新语句中添加一个 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,我还需要Update另外一个栏目用IF

4

3 回答 3

1
UPDATE scores t1
JOIN scores t2
ON FIND_IN_SET(t1.id, t2.vals)
SET t1.private = t1.private+1,
    CASE  procType 
    WHEN 3  THEN t1.regional = t1.regional+1
    WHEN 4  THEN t1.otherCol = t1.otherCol+1
    END as Col
WHERE t2.id = procId
于 2013-10-01T07:36:47.620 回答
1
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
于 2013-10-01T08:31:57.357 回答
1

将它们分成 2 个单独的更新语句会更简单,因为您要更新到不同的字段

IF procType = 3 THEN
    UPDATE scores t1
        JOIN scores t2
        ON FIND_IN_SET(t1.id, t2.vals)
    SET t1.private = t1.private+1,
     t1.regional = t1.regional+1;
    WHERE t2.id = procId;

ELSE IF procType = 4 THEN

    UPDATE scores t1
        JOIN scores t2
        ON FIND_IN_SET(t1.id, t2.vals)
    SET t1.private = t1.private+1,
        t1.otherCol = t1.otherCol+1;
    WHERE t2.id = procId;
于 2013-10-01T08:33:12.430 回答