Tim C
Tim C

Reputation: 5714

mysql procedure loop with increment

Please, know I'm learning MySQL as my application continues to grow. Please keep that in mind should you be kind enough to answer my question.

I'm trying to achieve the following

I added a new row to my table, keeping track of the game number in the round, thus it will have values 1,2,3,4.... when round ends it will be reset to 1 etc

I read in the MySQL manual about loops and came up with this, however it says my syntax is wrong, I would appreciate it if a more experienced user may look this over for me.

CREATE PROCEDURE inc()
    BEGIN
      DECLARE v1 INT 

      WHILE `round` ='1'

        SET v1 = v1 + 1;
    update events set `round_game_nr` ='v1'
      END WHILE;
    END;

I'm trying to achieve this

enter image description here

where 1st column is round and 2nd column is nr of games

Upvotes: 2

Views: 1930

Answers (1)

Yu Yenkan
Yu Yenkan

Reputation: 765

CREATE PROCEDURE inc()
BEGIN
  DECLARE v1 INT;    --semicolon missing

  WHILE `round` ='1' DO    --Do missing

    SET v1 = v1 + 1;
    UPDATE events SET `round_game_nr` =v1;   --semicolon missing, v1 should not with ``
  END WHILE;
END;

Upvotes: 1

Related Questions