CREATE 文
sql
1CREATE TABLE `score` (
2 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
3 `user_id` int(11) unsigned NOT NULL,
4 `score` int(11) unsigned DEFAULT NULL,
5 `create_at` datetime DEFAULT NULL,
6 PRIMARY KEY (`id`)
7) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Sample データ
sql
1INSERT INTO `score` (`id`, `user_id`, `score`, `create_at`)
2VALUES
3 (1,1,30,'2018-10-13 00:39:43'),
4 (2,1,70,'2018-10-13 00:39:43'),
5 (3,2,68,'2018-10-13 00:39:43'),
6 (4,2,43,'2018-10-13 00:39:43'),
7 (5,3,66,'2018-10-13 00:39:43'),
8 (6,2,10,'2018-10-13 00:39:43'),
9 (7,4,89,'2018-10-13 00:39:43'),
10 (8,3,129,'2018-10-13 00:39:43'),
11 (9,4,32,'2018-10-13 00:39:43');
ランキングを返すSQL
sql
1SELECT S.user_id, max(S.score) AS score, DATE(create_at) AS date, count(A.score) + 1 AS rank FROM (
2 SELECT user_id, sum(score) AS score, create_at FROM score
3 GROUP BY user_id
4 ORDER BY score DESC
5) S
6LEFT JOIN (
7 SELECT sum(score) AS score FROM score
8 GROUP BY user_id
9) A ON S.score < A.score
10GROUP BY S.user_id
11ORDER BY score DESC;
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/12 15:49