mysql求游戏排名

查看:82
本文介绍了mysql求游戏排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

1.数据结构

CREATE TABLE `active_gamescore` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `active_id` int(11) NOT NULL DEFAULT '0' COMMENT '关联active.id',
  `user_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '参与用户',
  `score` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '分数',
  `created_at` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间(时间戳)',
  PRIMARY KEY (`id`),
  KEY `active_id` (`active_id`),
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=240076 DEFAULT CHARSET=utf8 COMMENT='小游戏分数记录';

2.数据样例

id active_id user_id score created_at
240075 58 283 300 1483203758
193979 58 283 300 1483203759
66457 58 31 300 1483205803
227883 58 258 300 1483210053
24923 58 295 300 1483210380
97130 58 124 300 1483210871
83280 58 154 300 1483215285
96737 58 75 300 1483217001
114577 58 69 300 1483220107
68010 58 267 300 1483220189
23048 58 281 300 1483223949
168580 58 189 300 1483230406
143775 58 9 300 1483231469
84925 58 21 300 1483232858
161099 58 7 300 1483234003
52420 58 36 300 1483235310
145730 58 75 300 1483237517
67370 58 119 300 1483240950
161060 58 278 300 1483244220
92743 58 123 300 1483251658

3.求:分数最高的头20条记录的sql。
要求:
1.一个user_id只有一个名额,取最高分的那条;
2.同分的取时间靠前的那条

以上

解决方案

select user_id,a.sc,min(created_at) tm 
from (select user_id,max(score) sc from active_gamescore group by user_id) a 
join active_gamescore b 
on a.user_id=b.user_id and a.sc=b.score 
group by a.user_id,a.sc 
order by a.sc desc,tm asc limit 20;

这篇关于mysql求游戏排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆