【问题标题】:Select the highest row based on MySQL SUM result, with relations根据 MySQL SUM 结果选择最高行,有关系
【发布时间】:2012-08-23 01:12:05
【问题描述】:

我有以下MySQL 查询:

SELECT
s.student_id, s.student_firstname, s.student_lastname, s.isActive, 
    c.city_name,
    sd.student_startdate, sd.student_enddate,
    SUM(scpe.scpe_estemated_days) AS total
        FROM students s 
            INNER JOIN cityselections c ON c.city_id = s.student_city_id
            INNER JOIN studentdates sd ON sd.student_id = s.student_id
            LEFT JOIN studentcourseplan scp ON scp.student_id = s.student_id
            LEFT JOIN studentcourseplanelements scpe ON scpe.scpe_cpl_id = scp.cpl_id
                GROUP BY scp.cpl_id

这可以输出:

+------------+-------------------+------------------+----------+------------+-------------------+-----------------+-------+
| student_id | student_firstname | student_lastname | isActive | city_name  | student_startdate | student_enddate | total |
+------------+-------------------+------------------+----------+------------+-------------------+-----------------+-------+
| 83         | John              | Doe              | 1        | Dallas     | 2012-07-23        | 2012-09-30      | 413   |
| 84         | Derp              | Derpson          | 1        | Texas      | 2012-07-01        | 2012-08-26      | 413   |
| 85         | Barack            | Obama            | 1        | Washington | 2012-08-02        | 2012-08-31      | 2     |
| 85         | Barack            | Obama            | 1        | Washington | 2012-08-02        | 2012-08-31      | 153   |
+------------+-------------------+------------------+----------+------------+-------------------+-----------------+-------+

现在我只想为每个student_id 打印列total 中具有最高值的行

我尝试了MySQL MAX(),但无法成功。

应该怎么做?

【问题讨论】:

    标签: mysql sum left-join inner-join max


    【解决方案1】:

    尝试使用此查询...

    SELECT A.student_id, A.student_firstname, A.student_lastname, A.isActive, 
        A.city_name,
        A.student_startdate, A.student_enddate,
        MAX(A.total)
     FROM (SELECT
    s.student_id, s.student_firstname, s.student_lastname, s.isActive, 
        c.city_name,
        sd.student_startdate, sd.student_enddate,
        SUM(scpe.scpe_estemated_days) AS total
            FROM students s 
                INNER JOIN cityselections c ON c.city_id = s.student_city_id
                INNER JOIN studentdates sd ON sd.student_id = s.student_id
                LEFT JOIN studentcourseplan scp ON scp.student_id = s.student_id
                LEFT JOIN studentcourseplanelements scpe ON scpe.scpe_cpl_id = scp.cpl_id
                    GROUP BY scp.cpl_id) AS A
    GROUP BY A.student_id;
    

    希望对您有所帮助....

    【讨论】:

    • 完美!非常感谢。
    • 您的查询可能有效但不正确。您不应在选择列表中包含 GROUP BY 子句中未包含的属性。
    【解决方案2】:

    我建议使用子查询。

    SELECT s.student_id, s.student_firstname, s.student_lastname, s.isActive,
      c.city_name, sd.student_startdate, sd.student_enddate,
        (SELECT SUM(scpe.scpe_estemated_days) 
           FROM studentcourseplan scp
           LEFT JOIN studentcourseplanelements scpe ON scpe.scpe_cpl_id = scp.cpl_id
           WHERE scp.student_id = s.student_id) AS scpe_estemated_days
    FROM students s
    INNER JOIN cityselections c ON c.city_id = s.student_city_id
    INNER JOIN studentdates sd ON sd.student_id = s.student_id;
    

    fiddle

    我建议不要使用 shubhansh 的答案,因为它选择不包含在聚合中的属性,这是不好的做法。

    在标准 SQL 中,包含 GROUP BY 子句的查询不能引用 到选择列表中未在 GROUP BY 子句。例如,此查询在标准 SQL 中是非法的 因为选择列表中的名称列没有出现在 分组依据:

    SELECT o.custid, c.name, MAX(o.payment)
      FROM orders AS o, customers AS c
      WHERE o.custid = c.custid
      GROUP BY o.custid;
    

    MySQL 扩展了 GROUP BY 的使用,以便选择列表可以引用 未在 GROUP BY 子句中命名的非聚合列。这意味着 前面的查询在 MySQL 中是合法的。

    the manual

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 2013-11-18
      • 2012-03-30
      • 2013-11-09
      • 2022-08-10
      • 2014-12-23
      • 1970-01-01
      相关资源
      最近更新 更多