MySQL:如何通过 GROUP BY 字段来检索 ORDER BY 另一个字段的行?

发布于 2024-10-04 13:29:50 字数 1204 浏览 1 评论 0原文

假设以下数据:

数据:

    id  |   date        |   name    |   grade
--------+---------------+-----------+---------------
    1   | 2010/12/03    |   Mike    |   12
    2   | 2010/12/04    |   Jenny   |   12
    3   | 2010/12/04    |   Ronald  |   15
    4   | 2010/12/03    |   Yeni    |   11

我想知道每天谁的成绩最好,如下所示:

期望结果:

    id  |   date        |   name    |   grade
--------+---------------+-----------+---------------
    1   | 2010/12/03    |   Mike    |   12
    3   | 2010/12/04    |   Ronald  |   15

我认为查询应该如下所示:

SELECT name FROM mytable
GROUP BY date
ORDER BY grade DESC

但它返回类似这样的内容:

当前不需要的结果:

    id  |   date        |   name    |   grade
--------+---------------+-----------+---------------
    1   | 2010/12/03    |   Mike    |   12
    2   | 2010/12/04    |   Jenny   |   12

我搜索并找到了原因:

GROUP BY发生在ORDER BY之前,因此它看不到也无法应用ORDER。

所以我如何在 GROUP BY 上应用 ORDER?

注意:请记住,我需要最简单的查询,因为我的查询实际上非常复杂,我知道我可以通过一些子查询或联接来实现这个结果,但我想知道如何应用按顺序分组。谢谢

assume following data:

Data:

    id  |   date        |   name    |   grade
--------+---------------+-----------+---------------
    1   | 2010/12/03    |   Mike    |   12
    2   | 2010/12/04    |   Jenny   |   12
    3   | 2010/12/04    |   Ronald  |   15
    4   | 2010/12/03    |   Yeni    |   11

i want to know who has the best grade in each day, something like this:

Desired Result:

    id  |   date        |   name    |   grade
--------+---------------+-----------+---------------
    1   | 2010/12/03    |   Mike    |   12
    3   | 2010/12/04    |   Ronald  |   15

i thought query should look like this:

SELECT name FROM mytable
GROUP BY date
ORDER BY grade DESC

but it returns something like this:

Current Unwanted Result:

    id  |   date        |   name    |   grade
--------+---------------+-----------+---------------
    1   | 2010/12/03    |   Mike    |   12
    2   | 2010/12/04    |   Jenny   |   12

i searched and i found the reason:

GROUP BY happens before ORDER BY so it does not see and can't apply ORDER.

so how can i apply ORDER on GROUP BY?

Note: please keep in mind that i need the most simple query, because my query is actually very complex, i know i can achieve this result by some subquery or JOINing, but i want to know how to apply ORDER to GROUP BY. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是我以为 2024-10-11 13:29:50

我在这个例子中使用了 Oracle,但是 SQL 应该在 mysql 中工作(您可能需要调整 to_date 的内容才能与 mysql 一起工作)。您确实需要一个子查询来完成您所要求的操作。

CREATE TABLE mytable (ID NUMBER, dt DATE, NAME VARCHAR2(25), grade NUMBER);

INSERT INTO mytable VALUES(1,to_date('2010-12-03','YYYY-MM-DD'),'Mike',12);
INSERT INTO mytable VALUES(1,to_date('2010-12-04','YYYY-MM-DD'),'Jenny',12);
INSERT INTO mytable VALUES(1,to_date('2010-12-04','YYYY-MM-DD'),'Ronald',15);
INSERT INTO mytable VALUES(1,to_date('2010-12-03','YYYY-MM-DD'),'Yeni',11);

    SELECT id
         , dt
         , name
         , grade
      FROM mytable t1
     WHERE grade = (SELECT max(grade)
                      FROM mytable t2
                     WHERE t1.dt = t2.dt)
    ORDER BY dt

结果:

ID  DT          NAME   GRADE
1   12/3/2010   Mike   12
2   12/4/2010   Ronald 15

I used Oracle for this example, but the SQL should work in mysql (you may need to tweak the to_date stuff to work with mysql). You really need a subquery here to do what you are asking.

CREATE TABLE mytable (ID NUMBER, dt DATE, NAME VARCHAR2(25), grade NUMBER);

INSERT INTO mytable VALUES(1,to_date('2010-12-03','YYYY-MM-DD'),'Mike',12);
INSERT INTO mytable VALUES(1,to_date('2010-12-04','YYYY-MM-DD'),'Jenny',12);
INSERT INTO mytable VALUES(1,to_date('2010-12-04','YYYY-MM-DD'),'Ronald',15);
INSERT INTO mytable VALUES(1,to_date('2010-12-03','YYYY-MM-DD'),'Yeni',11);

    SELECT id
         , dt
         , name
         , grade
      FROM mytable t1
     WHERE grade = (SELECT max(grade)
                      FROM mytable t2
                     WHERE t1.dt = t2.dt)
    ORDER BY dt

Results:

ID  DT          NAME   GRADE
1   12/3/2010   Mike   12
2   12/4/2010   Ronald 15
梦途 2024-10-11 13:29:50

我知道您说过您想要一个仅 GROUP / ORDER 的解决方案,但在这种情况下您需要使用子查询。最简单的方法是这样的:

SELECT id, date, name, grade
FROM   mytable t1
WHERE grade = 
(SELECT MAX(t2.grade) FROM mytable t2 WHERE t1.id = t2.id)

这会显示多个学生是否分享了当天的最高成绩。

I know you said you wanted a GROUP / ORDER only solution but you will need to use a subquery in this instance. The simplest way would be something like this:

SELECT id, date, name, grade
FROM   mytable t1
WHERE grade = 
(SELECT MAX(t2.grade) FROM mytable t2 WHERE t1.id = t2.id)

This would show multiple students if they shared the highest grade for the day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文