简体   繁体   English

根据来自MySQL数据库的分数获取用户排名

[英]Get User Ranking based on points from MySQL Database

I have a function in my model of my codeigniter application that returns a list of rows in descending order of points. 我在我的codeigniter应用程序模型中有一个函数,该函数以点的降序返回行列表。 This is done to get the rows with the hihgest points. 这样做是为了获得具有最高点的行。

public function top_points()
    {
        $this->db->order_by('points', 'desc'); 
        $query=$this->db->get('player', 50);
        $data = $query->result_array();
        return $data;
    }

This work fine in returning the data for the highest 50 points. 这样可以很好地返回最高50分的数据。

However I would like to find the position/rank of a particular player in the Player table which I am querying. 但是我想在我查询的Player表中找到特定玩家的位置/排名。 I would like to a specific player by an id . 我想要一个id特定的玩家。

public function get_player_rank($player_id)
    {
            $this->db->where('player_id', $palyer_id); 
        $this->db->order_by('points', 'desc'); 
        $query=$this->db->get('player');
        $data = $query->result_array();
        return $data;
    }

I would like to use something like this to get the position of a player specified by his $id. 我想使用这样的方法来获取由他的$ id指定的玩家的位置。 Like a ranking in terms of his points earned. 就像他获得的积分排名一样。

How do i get this? 我怎么得到这个?

TRY 尝试

$sql = " 
SET @rownum=0;
SELECT player_id, rank 
FROM (
      SELECT player_id, @rownum := @rownum +1 AS rank FROM  `player` 
      ORDER BY  `points` DESC
     ) AS D
WHERE D.player_id=?";

$result = $this->db->query($sql,array($palyer_id))->row_array();

OR you could try 或者你可以尝试

$sql = " 
         SELECT player_id, rank 
         FROM (
                SELECT player_id, @rownum := @rownum +1 AS rank
                FROM  `player` , (SELECT @rownum :=0 ) r
                ORDER BY  `points` DESC
              ) AS D
         WHERE D.player_id=?";
$result = $this->db->query($sql,array($palyer_id))->row_array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM