在第一个表格中分组并保留第二个表格的最高值

时间:2016-09-09 21:43:02

标签: mysql group-by

这是我的表格

+----------+-----------+
|   id     |  user_id  |
+----------+-----------+
|    1     |     1     |
+----------+-----------+
|    2     |     1     |
+----------+-----------+
|    3     |     1     |
+----------+-----------+
|    4     |     2     |
+----------+-----------+
|    5     |     2     |
+----------+-----------+
|    6     |     2     |
+----------+-----------+
|    7     |     3     |
+----------+-----------+
|    8     |     3     |
+----------+-----------+
|    9     |     3     |
+----------+-----------+

我的第二张表

+----------+---------+
|   id     |  score  |
+----------+---------+
|    1     |    10   |
+----------+---------+
|    2     |    20   |
+----------+---------+
|    3     |     5   |
+----------+---------+
|    4     |    40   |
+----------+---------+
|    5     |    15   |
+----------+---------+
|    6     |    10   |
+----------+---------+
|    7     |     5   |
+----------+---------+
|    8     |    30   |
+----------+---------+
|    9     |    10   |
+----------+---------+

我需要从这些表格中选择用户达到的最高分数。

这是我的MySql查询

SELECT * FROM 
table_1 AS t1
INNER JOIN 
table_2 AS t2 ON 
t1.id = t2.id 
WHERE t2.score > 10
GROUP BY t1.user_id
ORDER BY t2.score DESC

我的愿望结果是

+----------+-----------+---------+
|   id     |  user_id  |  score  |
+----------+-----------+---------+
|    4     |      2    |    40   |
+----------+-----------+---------+
|    8     |      3    |    30   |
+----------+-----------+---------+
|    2     |      1    |    20   |
+----------+-----------+---------+

但我得到的是

+----------+-----------+---------+
|   id     |  user_id  |  score  |
+----------+-----------+---------+
|    4     |      2    |    40   |
+----------+-----------+---------+
|    1     |      1    |    10   |
+----------+-----------+---------+
|    7     |      3    |     5   |
+----------+-----------+---------+

当我使用id子句

时,MySql始终从table_1中选择最低GROUP BY

我尝试使用像这样的MAX命令

SELECT *, MAX(t2.score) AS max_score FROM 
table_1 AS t1
INNER JOIN 
table_2 AS t2 ON 
t1.id = t2.id 
WHERE t2.score > 10
GROUP BY t1.user_id
ORDER BY t2.score DESC
LIMIT 10

我得到的结果

+----------+-----------+---------+-----------+
|   id     |  user_id  |  score  | max_score |
+----------+-----------+---------+-----------+
|    4     |      2    |    40   |    40     |
+----------+-----------+---------+-----------+
|    1     |      1    |    10   |    20     |
+----------+-----------+---------+-----------+
|    7     |      3    |     5   |    30     |
+----------+-----------+---------+-----------+

我相信结果我希望它很容易获得,但我无处可去。

更新1

这个问题被标记为重复,但遗憾的是我无法在该页面上找到任何解决方案。

以下是我尝试的查询,但失败了。

SELECT * AS max_score FROM 
table_1 AS t1
INNER JOIN 
(
SELECT *, MAX(score) AS max_score
FROM table_2
GROUP BY t1.user_id
) AS t2
ON 
t1.id = t2.id 
WHERE t2.score > 10
ORDER BY t2.score DESC
LIMIT 10

它给我错误Unknown column t1.user_id

我试图从score中的table_2列中获取最高值,并将结果按user_id table_1分组。< / p>

这些页面上给出的示例仅针对一个表格,而我无法在我的方案中使用。

1 个答案:

答案 0 :(得分:1)

编写一个子查询,获取每个user_id的最高分数。然后将其与表格连接以获得具有该最大分数的行。

SELECT t1.id, t1.user_id, max_score
FROM table_1 AS t1
JOIN table_2 AS t2 ON t1.id = t2.id
JOIN (
    SELECT t1.user_id, MAX(t2.score) AS max_score
    FROM table_1 AS t1
    JOIN table_2 AS t2 on t1.id = t2.id
    GROUP BY t1.user_id) AS t_max
ON t1.user_id = t_max.user_id AND t2.score = t_max.max_score

DEMO