更新用户排名

时间:2017-08-17 13:25:44

标签: mysql mariadb

我正在尝试在单个查询中更新用户的排名,但它会因此错误而崩溃:

  

ER_PARSE_ERROR:您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,以便在'UPDATE用户附近使用正确的语法SET rank = @r:=(@ r + 1)ORDER BY得分DESC'在第1行

我的查询有什么问题吗?

    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.");
    });

我从this answer

收到了查询

我正在node.js mysql library上执行此查询。

import os
import zipfile
import glob

for path in glob.glob('./*/xxx*xxxx*'): ##walk the dir tree and find the files of interest

    zf = os.path.basename(path) #save the zipfile path
    zip_ref = zipfile.ZipFile(path, 'r') 
    zip_ref.extractall(path.replace(r'.zip', '')) #unzip to a folder without the .zip extension

2 个答案:

答案 0 :(得分:0)

怎么样:

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

我不确定你为什么要再次重新分配var。

答案 1 :(得分:0)

好的,我发现了问题。

我忘记将multipleStatements设置为true,如here中所述。