Home > Enterprise >  create a logic for the leaderboard with data in the MySQL table. Those who have the most points have
create a logic for the leaderboard with data in the MySQL table. Those who have the most points have

Time:12-21

You have to create a logic for the leaderboard with data in the MySQL table. Those who have the most points have to rank first. but If tied for points, whoever had the most total wins is ranked higher and if both have the same rank then show the same rank number and skip the next rank number for counting. Here are example data with rank.

Rank, username, Points, wins 

1: User A (100 points, 50 wins)

2: User B (96 points, 42 wins)

2: User C (96 points, 42 wins) 

4: User D (96 points, 41 wins)

5: User E (96 points, 40 wins)

6: User F (94 points, 42 wins)

6: User G (94 points, 42 wins)

6: User H (94 points, 42 wins)

9: User I (90 points, 40 wins)

10: User J (88 points, 41 wins)

i need this table in php

CodePudding user response:

Use the following query to directly get ranks from DB. It uses variables to keep track of ranks. number is for storing rank, prevPoints for storing previous points, prevWins for storing previous wins and curRank for storing actual rank.

SELECT @curRank := IF(@prevPoints=points AND @prevWins=wins, @curRank, @number) AS rank, points, wins,
@prevPoints:=points, @prevWins:=wins, @number:=@number 1
FROM users, (
SELECT @curRank :=0, @prevPoints:=null, @prevWins:=null, @number:=1
) r
ORDER BY points DESC, wins DESC
  • Related