1

目标

增加每个插入CATEGORYID,直到完成。15WHILE

场景

BEGIN
    DECLARE MaxAnnounces INT UNSIGNED DEFAULT 5;
    DECLARE Counter INT UNSIGNED DEFAULT 1;
    DECLARE NewWeek INT UNSIGNED DEFAULT 
            (SELECT WEEKID FROM announces_empire ORDER BY WEEKID DESC LIMIT 1);

    START TRANSACTION;
    WHILE Counter < MaxAnnounces DO
        INSERT INTO announces 
        (NAME, CAPTION, DESCRIPTION, IMAGEURL, LINK, CATEGORYID, WEEKID, YEARID)
        VALUES 
            ("Hello!", 
             "Click here.",
             "Can you tell me your name?",
             "example.com/img.jpg",
             "google.com",
             -- HERE I WANT TO INCREASE BY 1 EACH 5 INSERTS --
             40,
             2013);
        SET Counter = Counter + 1;
    END WHILE;
    COMMIT;
END

问题

我不知道语法。

问题亮点

而不是-- HERE I WANT TO INCREASE BY 1 EACH 5 INSERTS --我试图以这种方式做某事:

IF(Counter % 5, x + 1, 1),

x我还没有定义的变量在哪里。

Guilherme,你为什么不能用这个IF?怎么了?这样做插入的值总是1.

谁能给我一盏灯?

4

2 回答 2

1

将使用以下工作:

CAST((Counter / 5) AS UNSIGNED)

或者

FLOOR(Counter / 5)

或者

FORMAT((Counter / 5),0)

它看起来像下面这样:

VALUES 
  ("Hello!", 
  "Click here.",
  "Can you tell me your name?",
  "example.com/img.jpg",
  "google.com",
  CAST((Counter / 5) AS UNSIGNED),
  40,
  2013);
于 2013-10-17T20:56:45.283 回答
1

在你的一段时间之后,也许一开始就有这样的事情?

IF Counter % 5
BEGIN
    X = X + 1
END

并在您的插入中使用此变量。不要忘记声明 X。

于 2013-10-17T20:55:10.520 回答