Getting PHP MYSQL ranking query to rank based on total sum of score

后端 未结 2 762
你的背包
你的背包 2020-12-22 06:57
CREATE TABLE `players` (
 `pid` int(2) NOT NULL AUTO_INCREMENT,
 `name` varchar(50) NOT NULL,
`score` int(2) NOT NULL,
`game` varchar(20) NOT NULL,
PRIMARY KEY (`pid         


        
相关标签:
2条回答
  • 2020-12-22 07:57

    You want to use a group by statement rather than a subselect to solve this particular type of problem.

    0 讨论(0)
  • 2020-12-22 07:58

    You need to do the aggregation in a subquery and then use the variables to get the rank:

    select pid, name, game, score, (@rn := @rn + 1) as rank
    from (select pid, name, game, SUM(score) as score
          from player
          where game = 'aa'
          group by pid, game
         ) p cross join
         (select @rn := 0) vars
    order by score desc;
    
    0 讨论(0)
提交回复
热议问题