如何找到用户的国家排名和世界排名?

时间:2019-04-29 05:20:52

标签: mysql

如何找到用户的国家排名和世界排名? 请SQL查询如何制作

SELECT leaderboards.leaderboard_id, 
       users.name, 
       users.country, 
       users.points, 
       users.email, 
       leaderboards.score, 
       Find_in_set(users.points, (SELECT Group_concat(users.points ORDER BY 
                                         users.points DESC) 
                                  FROM   users))              AS world_rank, 
       Find_in_set(leaderboards.score, (SELECT 
       Group_concat(leaderboards.score ORDER BY 
       leaderboards.score DESC) 
                                        FROM   leaderboards)) AS rank, 
       leaderboards.created_date, 
       leaderboards.time_stamp 
FROM   users 
       INNER JOIN leaderboards 
               ON users.user_id = leaderboards.user_id 
WHERE  users.user_type = 2

上面的查询,我必须获得世界排名,但是如何找到国家排名

1 个答案:

答案 0 :(得分:0)

从排行榜ld联接中选择ld.leaderboard_id,c.name,c.points,c.world_rank,c.country_rank,ld.created_date,ld.time_stamp b。)从用户处获得b.points DESC)b))AS world_rank,FIND_IN_SET(un.points,(SELECT GROUP_CONCAT(un.points ORDER BY un.points DESC)from users un WHERE un.country = b.country))AS country_rank ,b。* FROM users un JOIN(SELECT u.name,u.user_type,u.country,u.points,u.user_id FROM users u)b ON b.user_id = un.user_id WHERE b.points!='' AND b.user_type = 2)c ON c.user_id = ld.user_id ORDER BY c.points DESC enter image description here