简体   繁体   English

在第一个表上分组,并保持第二个表的最大值

[英]Group by on first table and keep the highest value of the second table

Here are my tables 这是我的桌子

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

My second table 我的第二张桌子

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

I need to select the highest score achieved by a user from these tables. 我需要从这些表中选择用户获得的最高分数。

Here is my MySql query 这是我的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

My desire result is 我的愿望结果是

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

But what I get is 但是我得到的是

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

MySql is always selecting the lowest id from table_1 when I'm using the GROUP BY clause 当我使用GROUP BY子句时,MySql总是从table_1中选择最低的id

I tried using the MAX command like this 我尝试像这样使用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

And the result I'm getting 结果我得到了

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

I believe the result I wish it quite easy to get but I'm nowhere there. 我相信我希望很容易得到结果,但是我无处可去。

Update 1 更新1

This question was marked duplicate but unfortunately I couldn't find any solution on that given page. 这个问题被标记为重复,但是很遗憾,在给定的页面上我找不到任何解决方案。

Here is the query that I'm trying but it fails. 这是我正在尝试的查询,但失败。

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

It gives me the error Unknown column t1.user_id 它给我错误Unknown column t1.user_id

I'm trying to get the highest value from the column score which is in table_2 and group the results by user_id which is in table_1 . 我正在尝试从table_2的列score获取最高的值,并将结果按table_1 user_id分组。

The examples given on those pages target only one table and I'cant make that work on my scenario. 这些页面上给出的示例仅针对一个表,我无法在我的场景中进行。

Write a subquery that gets the max score for each user_id . 编写一个子查询,以获取每个user_id的最高分数。 Then join that with your tables to get the rows with that max score. 然后将其与您的表一起获得具有最高得分的行。

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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM