我在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 |
+--------+-------+
我正在尝试获得以下前3个得分:
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 BY子句的表达式#1不在GROUP BY子句中,并且包含未聚合的列'records.golf_scores.score',该列在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by
不兼容
将score
添加到GROUP BY
列表中只会返回最低的3个总分,而与person
列中的重复项无关。
如何在MySQL中获得所需的输出?
答案 0 :(得分:2)
由于order by子句在select子句之后执行,请尝试为min(score)设置别名。
SELECT person, MIN(score) as min_score FROM golf_scores GROUP BY person ORDER BY min_score LIMIT 3;
答案 1 :(得分:1)
您可以尝试使用$('.btn-add').on('click', function(){
... do someting ...
$('.first_two_guesses').hide();
$('.word_guesses').show();
}
$('.btn-guess').on('click', function(){
... do the same thing ...
}
row_number()