相比于常规的group by 此处使用over开窗更方便 首先利用开窗对每个用户的总积分求和,然后使用order by 排序,再通过limit取得总积分最高的人,最后再通过user表查找姓名即可
select name,grade_num from
(select user_id,sum(grade_num) over(partition by user_id) as
grade_num from grade_info order by grade_num desc limit 1) as t,
user u
where t.user_id = u.id