根据MySQL SUM结果选择最高行,并使用关系

时间:2012-08-28 13:52:23

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

我有以下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   |
+------------+-------------------+------------------+----------+------------+-------------------+-----------------+-------+

现在,我只想为每个total

列打印student_id列中值最高的行

我尝试了MySQL MAX(),但我无法让它发挥作用。

应该怎么做?

2 个答案:

答案 0 :(得分:0)

尝试使用此查询...

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;

希望它有所帮助......

答案 1 :(得分:0)

我建议使用子查询。

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中是非法的   因为选择列表中的名称列不会出现在   GROUP BY:

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