【问题标题】:Show top N rows by category in MySQL 8 without duplicates in another category在 MySQL 8 中按类别显示前 N 行,而在另一个类别中没有重复
【发布时间】:2019-02-15 13:28:13
【问题描述】:

类似于this question,我在 MySQL 8.0.15 中有下表:

CREATE TABLE golf_scores (id INT PRIMARY KEY AUTO_INCREMENT, person TEXT, score INT, age INT);
INSERT INTO golf_scores (person, score, age) VALUES ('Angela', 40, 25),('Angela', 45, 25),('Angela', 55, 25),('Peter',45, 32),('Peter',55,32),('Rachel', 65, 35),('Rachel',75,35),('Jeff',75, 16);
SELECT * FROM golf_scores;
+----+--------+-------+------+
| id | person | score | age  |
+----+--------+-------+------+
|  1 | Angela |    40 |   25 |
|  2 | Angela |    45 |   25 |
|  3 | Angela |    55 |   25 |
|  4 | Peter  |    45 |   32 |
|  5 | Peter  |    55 |   32 |
|  6 | Rachel |    65 |   35 |
|  7 | Rachel |    75 |   35 |
|  8 | Jeff   |    75 |   16 |
+----+--------+-------+------+

我们要选择以下“最佳”3 行:

+----+--------+-------+------+
| id | person | score | age  |
+----+--------+-------+------+
|  1 | Angela |    40 |   25 |
|  4 | Peter  |    45 |   32 |
|  6 | Rachel |    65 |   35 |
+----+--------+-------+------+

换句话说,没有重复的最低 3 个高尔夫分数,以及该行的其他列。我不担心关系;我仍然想要三个结果。

查询 SELECT person, MIN(score) as min_score FROM golf_scores GROUP BY person ORDER BY min_score LIMIT 3; 提供正确的行,但仅限于列 person 和 score`。当我尝试这样修改它时:

SELECT id, person, MIN(score) as min_score, age FROM golf_scores GROUP BY person ORDER BY min_score LIMIT 3;

我收到此错误:

错误 1055 (42000):SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含非聚合列“records.golf_scores.id”,它在功能上不依赖于 GROUP BY 子句中的列;这与 sql_mode=only_full_group_by 不兼容

我也尝试使用 SELECT id, DISTINCT person, score, age FROM golf_scores ORDER BY score LIMIT 3 消除重复名称,但出现错误

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“DISTINCT person, score FROM golf_scores ORDER BY score LIMIT 3”附近使用正确的语法

如何在 MySQL 中获得所需的输出?

【问题讨论】:

  • 避免在 cmets 中回答问题。
  • MySQL 8 版有窗口函数!
  • 为什么一个人的年龄会存储在分数旁边?
  • 为什么不用 person_id 而不是人名。
  • @SalmanA,这是一个 MWE,旨在代表一个真实的应用程序,我没有尽最大努力减少。也许像temperature 这样的专栏会更好,因为这是高尔夫球场的一个特点,而不是人。无论哪种方式,它只是一个表,我们希望返回其中的所有行。不过,感谢您指出这一点!

标签: mysql sql group-by distinct mysql-8.0


【解决方案1】:

使用row_number():

select t.*
from (select t.*, row_number() over (partition by person order by score) as seqnum
      from golf_scores  t
     ) t
where seqnum = 1
order by score asc
limit 3;

在旧版本中,您可以使用相关子查询和id

select gs.*
from golf_scores gs
where gs.id = (select gs2.id
               from golf_scores gs2
               where gs2.person = gs.person
               order by gs2.score asc
               limit 1
              )
order by score asc
limit 3;

这也可能是在golf_scores(person, score, id) 上建立索引的最快方式。

【讨论】:

  • 我是不是做错了什么,还是需要在末尾加上LIMIT 3 才能在前 3 个分数处将其截断?
【解决方案2】:

这是一种方法:

SELECT x.* 
  FROM golf_scores x
  JOIN 
     ( SELECT MIN(id) id FROM
            ( SELECT a.* 
                FROM golf_scores a 
                JOIN 
                   ( SELECT person, MIN(score) score FROM golf_scores GROUP BY person ) b 
                  ON b.person = a.person 
                 AND b.score = a.score 
            ) n
        GROUP
           BY person
            , score 
     ) y
    ON y.id = x.id
 ORDER 
    BY x.score LIMIT 3;

【讨论】:

    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2021-01-09
    相关资源
    最近更新 更多