我试图在我的街机游戏室中设置一个高分板。我的目标是从表GAME
中显示每个游戏的前5名玩家。
我创建了三个表:
表格游戏:
+----------+--------------+
| id_game | name_game |
+----------+--------------+
| 1 | PAC MAN |
| 2 | GALAXIAN |
| 3 | XEVIOUS |
表PLAYER:
+----------+---------------+
| id_player | name_player |
+----------+---------------+
| 1 | BRUNO |
| 2 | STEVE |
表SCORE:
+----------+----------+-----------+------+------+
| id_score | id_game | id_player | date | score |
+----------+----------+-----------+------+------+
| 1 | 1 | 1 | 01/10/15 | 230234 |
| 2 | 1 | 2 | 04/10/15 | 120234 |
| 3 | 1 | 1 | 03/10/15 | 440224 |
| 4 | 1 | 1 | 06/10/15 | 200000 |
| 5 | 1 | 2 | 09/10/15 | 330233 |
| 6 | 1 | 1 | 01/10/15 | 510000 |
| 7 | 2 | 1 | 01/10/15 | 730874 |
| 8 | 2 | 2 | 01/10/15 | 990900 |
| 9 | 3 | 1 | 01/10/15 | 444000 |
我想帮助构建sql代码来生成这样的东西:
PAC MAN
1. BRUNO 510000 PTS 01/10/15
2. STEVE 330233 PTS 09/10/15
3. XXXXX 230233 PTS 02/10/14
4. YYYYY 130233 PTS 06/10/15
5. ZZZZZ 030233 PTS 10/10/13
GALAXIAN
1. STEVE 990900 PTS 01/10/15
2. BRUNO 730874 PTS 01/10/15
3. XXXXX 230233 PTS 02/10/14
4. YYYYY 130233 PTS 06/10/15
5. ZZZZZ 030233 PTS 10/10/13
如果同一个玩家在同一个游戏中获得多个分数,则只应显示他/她的最佳分数。你能帮我设置代码吗?
答案 0 :(得分:1)
你会想要这样的东西。第一个查询可能是您实际用于对自己的表运行它的查询。第二个例子,你可以直接在mysql中运行,并根据需要捏造数据。
select
g.name_game,
p.name_player,
max(s.score),
s.date
from
game g
inner join score s on
s.id_game = g.id_game
inner join player p on
p.id_player = s.id_player
where
g.name_game = 'PAC MAN'
group by
g.name_game,
p.name_player
ORDER BY
s.score asc
LIMIT
5;
select
g.name_game,
p.name_player,
max(s.score),
s.date
from
(
select 1 id_game, 'PAC MAN' name_game union all
select 2 id_game, 'GALAXIAN' name_game union all
select 3 id_game, 'XEVIOUS' name_game
) g
inner join (
select 1 id_score, 1 id_game, 1 id_player, '01/10/15' date, 230234 score union all
select 2 id_score, 1 id_game, 2 id_player, '04/10/15' date, 120234 score union all
select 3 id_score, 1 id_game, 1 id_player, '03/10/15' date, 440224 score union all
select 4 id_score, 1 id_game, 1 id_player, '06/10/15' date, 200000 score union all
select 5 id_score, 1 id_game, 2 id_player, '09/10/15' date, 330233 score union all
select 6 id_score, 1 id_game, 1 id_player, '01/10/16' date, 410000 score union all
select 6 id_score, 1 id_game, 1 id_player, '01/10/15' date, 510000 score union all
select 7 id_score, 2 id_game, 1 id_player, '01/10/15' date, 730874 score union all
select 8 id_score, 2 id_game, 2 id_player, '01/10/15' date, 990900 score union all
select 9 id_score, 3 id_game, 1 id_player, '01/10/15' date, 444000 score
) s on
s.id_game = g.id_game
inner join (
select '1' id_player, 'BRUNO' name_player union all
select '2' id_player, 'STEVE' name_player
) p on
p.id_player = s.id_player
where
g.name_game = 'PAC MAN'
group by
g.name_game,
p.name_player
ORDER BY
s.score asc
LIMIT
5;
也许下次发帖你试过?
如果您想在同一个查询中使用所有组,也可以使用组。只需将[game_name]替换为您想要的游戏名称即可。
答案 1 :(得分:1)
SELECT MAX(s.score) AS score, p.name_player, g.name_game
FROM GAME g
JOIN SCORE s ON s.id_game = g.id_game
JOIN PLAYER p ON p.id_player = s.id_player
GROUP BY p.id_player, g.id_game
ORDER BY g.id_game, score
好吧,我上面的查询仍有问题:-(
查询显示每个唯一玩家的最佳得分,但相关日期不正确。 “日期”字段仍然显示每位玩家完成的第一个分数的第一个日期。
让我来说明一下。
我只需将DATE值添加到查询的第一行,即:
SELECT MAX(s.score) AS score, p.name_player, g.name_game, date
FROM GAME g
JOIN SCORE s ON s.id_game = g.id_game
JOIN PLAYER p ON p.id_player = s.id_player
GROUP BY p.id_player, g.id_game
ORDER BY g.id_game, score
+----------+----------+-----------+------+------+
| id_score | id_game | id_player | date | score |
+----------+----------+-----------+------+------+
| 1 | 1 | 1 | 01/10/12 | 230234 |
| 2 | 1 | 1 | 04/10/13 | 120234 |
| 3 | 1 | 1 | 03/10/14 | 440224 |
您的查询将返回此信息:
440224 1 1 01/10/12
但我期待这个:440224 1 1 03/10/14
我试着在这里添加max(date),但我觉得显然有些事情比较复杂吗?