1

我有以下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()了,但我无法让它工作。

应该怎么做?

4

2 回答 2

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;

希望能帮助到你....

于 2012-08-28T14:33:23.263 回答
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;

小提琴

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

在标准 SQL 中,包含 GROUP BY 子句的查询不能引用选择列表中未在 GROUP BY 子句中命名的非聚合列。例如,这个查询在标准 SQL 中是非法的,因为选择列表中的 name 列没有出现在 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 中是合法的。

请参阅手册

于 2012-08-28T14:54:06.667 回答