基于Group By中最大列选择行

时间:2013-10-04 17:58:09

标签: mysql sql

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

有一个users表:

enter image description here

scores表:

enter image description here

我尝试过使用此查询:

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

返回此结果(示例):

enter image description here

正如您所看到的,根据{{FUNTIMES,用户210的得分eagles归因于vikings。 1}} table(scores的{​​{1}}是11)。

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

FUNTIMES

但是,当然,我无法选择WHERE user_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 。我完全迷失了。

提前感谢您的帮助。

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

3 个答案:

答案 0 :(得分:3)

SQL Fiddle

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

<强> Results

| 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`

<强> Results

| USER_ID | MAX_SCORE |
|---------|-----------|
|       9 |       610 |
|      10 |       290 |
|      11 |       210 |
|      12 |       170 |

答案 1 :(得分: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)

答案 2 :(得分:0)

这些方面应该有效:

UPDATE结果SET max_score =(SELECT SUM(得分)FROM得分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)LIMIT 50;