gpt4 book ai didi

mysql - 对第一个表进行分组并保留第二个表的最高值

转载 作者:行者123 更新时间:2023-11-29 11:18:04 25 4
gpt4 key购买 nike

这是我的表格

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

我的第二张 table

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

当我使用 GROUP BY 子句时,MySql 始终从 table_1 中选择最低的 id

我尝试使用这样的 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

它给了我错误未知列t1.user_id

我试图从 table_2 中的 score 列中获取最高值,并按 user_id 中的 user_id 对结果进行分组表_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

关于mysql - 对第一个表进行分组并保留第二个表的最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39420117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com