0

I'm trying to update users' rankings in a single query but it crashes with this error:

ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE users SET rank = @r:= (@r+1) ORDER BY score DESC' at line 1

Is there anything wrong with my query?

SET @r=0;UPDATE users SET rank = @r:= (@r+1) ORDER BY score DESC

I got the query from this answer

I'm executing this query on a node.js mysql library.

    db.query('SET @r=0;UPDATE users SET rank = @r:= (@r+1) ORDER BY score DESC', function(err){
        if (err){
            console.log(err);
            return;
        }
        console.log("Ranking updated successfully.");
    });
user234
  • 99
  • 1
  • 1
  • 8

2 Answers2

0

How about:

SET @r=0;UPDATE users SET rank = (@r+1) ORDER BY score DESC

I'm not sure as why you would want to reassign the var again.

Peter M
  • 1,059
  • 8
  • 19
0

Ok, i've found the problem.

I forgot to set multipleStatements to true as described in here.

user234
  • 99
  • 1
  • 1
  • 8