如何对分数进行排名但忽略某些值。使用mysql

时间:2016-01-13 16:25:06

标签: php mysql node.js

好的,我有一个问题,我使用elo系统来创建排名系统。我也有一个变量用于他们所玩的比赛数量,但我想从排名中删除少于5场比赛的人。

所以我有名字,现在的elo,玩过的游戏数量。我想排名然后下降(高elo更好)但忽略所有人数少于5的游戏。

我在程序的其余部分使用nodejs,我的所有数据都存储在一个mysql数据库中。对于拥有相同elo的人来说,我的排名系统也需要有联系。

现在我在家,我可以添加我的代码。 我的数据库如下所示:

Database

它有点复杂,但这就是它的样子。

我的代码



exports.getUserRank = function(userid, ladder, callback){
    getSingleRow("SELECT `gamesplayed`, `userid` FROM `users` WHERE `userid` = ?", userid, function(played) {
        console.log(played.gamesplayed);
        if(played.gamesplayed > 10){
            makeQuery("SELECT `userid`, `ladder_" + ladder + "_rating`, rank FROM (SELECT `userid`, `gamesplayed`, `ladder_" + ladder + "_rating`," +
                "@curRank := IF(@prevRank = `ladder_" + ladder + "_rating`, @curRank, @incRank) AS rank,  @incRank := @incRank + 1, " +
                " @prevRank := `ladder_" + ladder + "_rating` FROM `users` p, ( SELECT @curRank :=0, @prevRank := NULL, @incRank := 1)  " +
                " r  ORDER BY `ladder_" + ladder + "_rating` DESC) s WHERE `userid` = ?", userid, function(data) {
                if (data && data[0] && data[0].rank) {
                    callback(data[0].rank);
                }
            });
        }
        else {
            if(played.gamesplayed < 10){
                callback("lessthan10");
            }
            else{
            callback(null)
            }
        }
    });
};
&#13;
&#13;
&#13;

现在我不确定但是php会是这样的:

SELECT `userid`, `ladder_" + ladder + "_rating`, rank 
FROM (SELECT `userid`, `gamesplayed`, `ladder_ + ladder + "_rating`,
@curRank := IF(@prevRank = `ladder_" + ladder + "_rating`, @curRank, @incRank) AS rank,
@incRank := @incRank + 1, 
@prevRank := `ladder_" + ladder + "_rating` FROM `users` p,
(SELECT @curRank :=0, @prevRank := NULL, @incRank := 1)r  
ORDER BY `ladder_" + ladder + "_rating` DESC) s WHERE `userid` = ?

既然我的代码在这里,我希望有人可以帮助我。

2 个答案:

答案 0 :(得分:1)

如果您已经知道如何计算等级

 SELECT *, rank
 FROM allTable

非常简单,只需过滤原始表格

SELECT *, rank
FROM ( SELECT *
       FROM allTable
       WHERE NoOfgames >=5
     ) as filterTable

答案 1 :(得分:0)

好的,我设法解决了我的问题,部分归功于@Juan Carlos Oropeza谢谢。

exports.getUserRank = function(userid, ladder, callback){
    getSingleRow("SELECT `gamesplayed`, `userid` FROM `users` WHERE `userid` = ?", userid, function(played) {
        console.log(played.gamesplayed);
        if(played.gamesplayed > 10){
            makeQuery("SELECT `userid`, `ladder_" + ladder + "_rating`, rank FROM (SELECT `userid`, `gamesplayed`, `ladder_" + ladder + "_rating`," +
                "@curRank := IF(@prevRank = `ladder_" + ladder + "_rating`, @curRank, @incRank) AS rank,  @incRank := @incRank + 1, " +
                " @prevRank := `ladder_" + ladder + "_rating` FROM (SELECT `userid`, `gamesplayed`, `ladder_" + ladder + "_rating` FROM `users` WHERE `gamesplayed` >=10) p," +
                " ( SELECT @curRank :=0, @prevRank := NULL, @incRank := 1)  " +
                " r  ORDER BY `ladder_" + ladder + "_rating` DESC) s WHERE `userid` = ?", userid, function(data) {
                if (data && data[0] && data[0].rank) {
                    callback(data[0].rank);
                }
            });
        }
        else {
            if(played.gamesplayed < 10){
                callback("lessthan10");
            }
            else{
                callback(null)
            }
        }
    });
};

希望这有助于任何需要它的人。