假设我有一个存放学生分数的表,有两个字段,
student_id
和score
,假设我每天存一次,但是存的分数不一样。现在我要查所有记录,结果如下:
[
{'student_id':5, 'score': 7},
{'student_id':5, 'score': 3},
{'student_id':6, 'score': 9},
{'student_id':6, 'score': -2},
{'student_id':7, 'score': 8},
{'student_id':7, 'score': 6},
]
现在我有一个需求,查询所有信息,用 student_id
分组,并将相同的 student_id 的 score 相加,然后 score 倒序,结果如下:
[
{'student_id':7, 'score': 14},
{'student_id':5, 'score': 10},
{'student_id':6, 'score': 7},
]
求大神解答!
select student_id,sum(score) from 表名 group by student_id order by score desc