需要对不同的行进行求和,而不是明确的值

时间:2012-09-27 10:41:54

标签: php mysql

+------+------------+----------+--------+
|  id  |  course    |   std_id |  marks |
+------+------------+----------+--------+
|  1   |     1      |    1     |   9    |
|  2   |     2      |    2     |   5    |
|  3   |     1      |    3     |   5    |
|  4   |     1      |    1     |   6    |
|  5   |     2      |    1     |   4    |
+------+------------+----------+--------+


+----------------+----------------+
| course         |  credit        |
+----------------+----------------+
|    1           |     5          |
|    2           |     5          |
+----------------+----------------+

我需要通过desc获得学生标记和学分不同的课程顺序

像:

+---------+------------+-------+
|std_id   |  sum(marks)|  sum()|
+---------+------------+-------+
|  1      |     10     |    10 |
|  2      |     5      |    5  |
|  3      |     5      |    5  |
+---------+------------+-------+

1 个答案:

答案 0 :(得分:0)

我对你的问题和你的头衔有点困惑。这个答案应该可以帮助你解决你在标题中提出的问题 - 但是你发布的一些输出很令人费解:

select
    c.std_id,
    sum(c.marks) as marks,
    sum(c.credit) as credits
from
    (
        select distinct
            a.id,
            a.course,
            a.std_id,
            a.marks,
            b.credit
        from
            marks a
                join courseCredits
                    on a.course=b.course
    ) c
group by
    c.std_id,
    c.course,
order by
    std_id desc