【问题标题】:Mysql select query count until reach the condition with conditionMysql 选择查询计数,直到达到有条件的条件
【发布时间】:2016-11-23 10:24:39
【问题描述】:

我有包含他的积分和游戏 ID 的用户列表。我需要根据max(lb_point)的游戏顺序找到指定用户的排名。

我已经完成了基于单个游戏获得排名的查询,如下所示。

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

但我需要根据整体游戏找到排名。示例我有 3 种不同的游戏(1,2,3)。通过传递 user_id,我需要找到他在所有三场比赛中的总体排名。你能帮我解决这个问题吗?

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     

【问题讨论】:

  • 对于 User_ID 您需要绝对最大排名还是每个游戏 ID 的 raknk?向我展示所提供样本的预期结果

标签: mysql count conditional-statements delimiter


【解决方案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

最里面的查询获取每个用户每场比赛的最高分并将其相加。 下一个查询根据每个用户的总分分配一个排名。 最外层的查询选择用户。

【讨论】:

  • 非常感谢兄弟:)
  • 感谢您的大力帮助。我需要另一个查询,前 10 名用户列表,基于所有游戏的积分。你能帮我解决这个问题吗?
  • 使用相同的查询将最后一个 where 语句更改为 LIMIT 10。
猜你喜欢
  • 2016-11-16
  • 2016-11-16
  • 2012-10-21
  • 2016-03-21
  • 2011-08-10
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多