Mysql select query count until reach the condition with condition

梦想的初衷 提交于 2019-12-08 13:37:07

问题


I have lists of users with his points and game id. I need to find the rank of the specified user based on the game order by the max(lb_point).

I have already done the query for getting the rank based on individual game as follows.

select count(*) AS user_rank 
                                        from (
                                              select distinct user_id
                                              from leader_board 
                                              where   lb_point >= (select  max( lb_point )
                                                     from leader_board 
                                                     where user_id = 1   
                                                     and game_id = 2 )
                                              and game_id = 2
                                        ) t

But i need to find the rank based on the overall game. Example i have 3 different games (1,2,3). By passing the user_id, i need to find his overall rank among all three games. Can you please help me on this?

lb_id    user_id   game_id   lb_point
------------------------------------------------
1         1        2         670     
2         1        1         200     
3         1        2         650     
4         1        1         400     
5         3        2         700     
6         4        2         450     
7         2        1         550     
8         2        1         100     
9         1        1         200    
10        2        1         100     
11        1        1         200     
12        2        1         100     
13        1        1         200     
14        2        1         100     
15        1        1         200     
16        2        1         100     
17        1        1         200     
18        2        1         100     
19        1        1         200     
20        2        1         100     
21        1        1         200     
22        2        1         800     

回答1:


use sandbox;
/*create table t (lb_id int,   user_id int,  game_id int,   lb_point int);
truncate table t;
insert into t values
(1 ,        1,        2,         670),     
(2 ,        1,        1,         200),    
(3 ,        1,        2,         650),     
(4 ,        1,        1,         400),     
(5 ,        3,        2,         700),     
(6 ,        4,        2,         450),     
(7 ,        2,        1,         550),     
(8 ,        2,        1,         100),     
(9 ,        1,        1,         200),    
(10,        2,        1,         100),     
(11,        1,        1,         200),     
(12,        2,        1,         100),     
(13,        1,        1,         200),     
(14,        2,        1,         100),     
(15,        1,        1,         200),     
(16,        2,        1,         100),     
(17,        1,        1,         200),     
(18,        2,        1,         100),     
(19,        1,        1,         200),     
(20,        2,        1,         100),     
(21,        1,        1,         200),     
(22,        2,        1,         800);  

*/

select t.*
from
(
select s.*,@rn:=@rn+1 as rank
from
(
select  user_id, sum(lb_point)  points
from        t 
where       lb_id = (select t1.lb_id from t t1 where t1.user_id = t.user_id and t1.game_id = t.game_id order by t1.lb_point desc limit 1)
group   by user_id
order       by points desc
) s
,(select @rn:=0) rn
) t
where   t.user_id = 1

The innermost query grabs the highest score per game per user and sums it. The next query assigns a rank based on the aggregated score per user. The outermost query selects the user.



来源:https://stackoverflow.com/questions/38476916/mysql-select-query-count-until-reach-the-condition-with-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!