我在MySQL 8.0.15中有下表:
CREATE TABLE golf_scores (person TEXT, score INT);
INSERT INTO golf_scores VALUES ('Angela', 40),('Angela', 45),('Angela', 55),('Peter',45),('Peter',55),('Rachel', 65),('Rachel',75),('Jeff',75);
SELECT * FROM golf_scores;
+--------+-------+
| person | score |
+--------+-------+
| Angela | 40 |
| Angela | 45 |
| Angela | 55 |
| Peter | 45 |
| Peter | 55 |
| Rachel | 65 |
| Rachel | 75 |
| Jeff | 75 |
+--------+-------+我正在努力取得以下前三名的分数:
SELECT * FROM golf_scores;
+--------+-------+
| person | score |
+--------+-------+
| Angela | 40 |
| Peter | 45 |
| Rachel | 65 |
+--------+-------+换句话说,我想要最好的(最低的)3的高尔夫得分,而不是由人重复。我不担心关系,我还是想要三个结果。
我认为这个查询可以做到:
SELECT person, MIN(score) FROM golf_scores GROUP BY person ORDER BY score LIMIT 3;但我得到了以下错误:
错误1055 (42000):ORDER子句的表达式#1不是按子句分组,而是包含非聚合列'records.golf_scores.score‘,它在功能上不依赖于按子句分组的列;这与sql_mode=only_full_group_by不兼容
将score添加到GROUP BY列表中只返回最低的3分总数,而不考虑person列中的重复项。
如何在MySQL中获得所需的输出?
发布于 2019-02-15 03:32:15
由于order子句是在select子句之后执行的,所以尝试将别名放在min(得分)中。
SELECT person, MIN(score) as min_score FROM golf_scores GROUP BY person ORDER BY min_score LIMIT 3;
发布于 2019-02-15 03:16:18
您可以尝试使用row_number()
select * from
(
SELECT person, score,row_number() over(partition by person order by score) as rn
FROM golf_scores
)A where rn=1
ORDER BY score LIMIT 3https://stackoverflow.com/questions/54702226
复制相似问题