3

我正在尝试为视频游戏创建一个高分板。

有一个users表:

在此处输入图像描述

还有一张scores桌子:

在此处输入图像描述

我试过使用这个查询:

SELECT MAX(`scores`.`score`) AS max_score,
    `scores`.`team`,
    `users`.`name`
FROM `scores`
    LEFT JOIN `users`
    ON `users`.`id` = `scores`.`user_id`
GROUP BY `scores`.`user_id`
ORDER BY max_score DESC
LIMIT 50

返回此结果(示例):

在此处输入图像描述

如您所见, userFUNTIMES的得分210被归因于根据表格eagles应该归因于的时间( 's是 11)。vikingsscoresFUNTIMESuser_id

我将如何解决这个问题?像这样的东西会很好地工作:

SELECT `scores`.`score`, `scores`.`team`, `users`.`name`
FROM `scores`
LEFT JOIN `users`
ON `users`.`id` = `scores`.`user_id`
WHERE `scores`.`id` IN (
    SELECT MAX(`scores`.`score`)
    FROM `scores`
    GROUP BY `scores`.`user_id`
)
ORDER BY `scores`.`score` DESC
LIMIT 50

但是,当然,我不能选择 WHERE idis IN score。我完全迷路了。

提前感谢您的帮助。

编辑:对不起,我忘了提:每个用户只能在高分表中显示一次。(这就是为什么我的user_id查询中有 GROUP BY 规定的原因。)

4

3 回答 3

3

SQL小提琴

MySQL 5.5.32 架构设置

CREATE TABLE users 
    (`id` int, `name` varchar(8), `pass` varchar(4))
;

INSERT INTO users 
    (`id`, `name`, `pass`)
VALUES
    (2, 'JAKE', 'jake'),
    (3, NULL, NULL),
    (4, 'AWNO', 'ffff'),
    (5, 'FFF', 'ffff'),
    (6, 'ASDF', 'asdf'),
    (7, 'KKK', 'kkkk'),
    (8, 'AW YEAH', 'good'),
    (9, 'FFFF', 'ffff'),
    (10, 'FUNTIME', 'llll'),
    (11, 'FUNTIMES', 'llll'),
    (12, 'GOOD', 'good')
;

CREATE TABLE scores
    (`id` int, `user_id` int, `team` varchar(7), `score` int)
;

INSERT INTO scores
    (`id`, `user_id`, `team`, `score`)
VALUES
    (32, 9, 'vikings', 610),
    (33, 10, 'eagles', 290),
    (34, 11, 'eagles', 0),
    (35, 11, 'vikings', 40),
    (36, 11, 'vikings', 210),
    (37, 12, 'eagles', 170),
    (38, 12, 'eagles', 30)
;

查询 1

SELECT m.`max_score`,
    s.`team`,
    u.`name`
FROM `scores` s
    LEFT JOIN `users` u
    ON u.`id` = s.`user_id`
INNER JOIN (SELECT `user_id`, MAX(`score`) as max_score 
            FROM `scores` 
            GROUP BY `user_id`) m ON m.`user_id` = s.`user_id` AND
                                     m.`max_score` = s.`score`
ORDER BY max_score DESC
LIMIT 50

结果

| MAX_SCORE |    TEAM |     NAME |
|-----------|---------|----------|
|       610 | vikings |     FFFF |
|       290 |  eagles |  FUNTIME |
|       210 | vikings | FUNTIMES |
|       170 |  eagles |     GOOD |

讨论目的:

查询 1

SELECT `user_id`, MAX(`score`) as max_score 
            FROM `scores` 
            GROUP BY `user_id`

结果

| USER_ID | MAX_SCORE |
|---------|-----------|
|       9 |       610 |
|      10 |       290 |
|      11 |       210 |
|      12 |       170 |
于 2013-10-04T18:09:51.243 回答
2
SELECT scores.score, scores.team, users.name
FROM users
JOIN scores
  ON users.id = scores.user_id
WHERE NOT EXISTS 
  (SELECT *
   FROM scores scores2
   WHERE scores2.score > scores.score
     AND scores2.user_id = users.id)
于 2013-10-04T18:12:33.287 回答
0

这些方面的东西应该起作用:

更新结果 SET max_score = (SELECT SUM(score) FROM scores WHERE users.id=scores.user_id), team = (SELECT team FROM scores WHERE scores.user_id=users.id), name = (SELECT name from users WHERE users. id=scores.user_id) 限制 50;

于 2013-10-04T18:15:46.563 回答