题解 | #获得积分最多的人(二)#

获得积分最多的人(二)

https://www.nowcoder.com/practice/b6248d075d2d4213948b2e768080dc92

select user.id, user.name,t.grade_sum from(
select user_id,sum(grade_num) as grade_sum from grade_info group by user_id
having grade_sum=(select sum(grade_num) as grade_sum from grade_info group by user_id order by grade_sum desc limit 1) 
) t
join user
on t.user_id=user.id
order by user.id

获取最大值

select sum(grade_num) as grade_sum from grade_info group by user_id order by grade_sum desc limit 1

获取和最大值一样的用户

select user_id,sum(grade_num) as grade_sum from grade_info group by user_id
having grade_sum=(select sum(grade_num) as grade_sum from grade_info group by user_id order by grade_sum desc limit 1) 

在联立user表可得

select user.id, user.name,t.grade_sum from(
select user_id,sum(grade_num) as grade_sum from grade_info group by user_id
having grade_sum=(select sum(grade_num) as grade_sum from grade_info group by user_id order by grade_sum desc limit 1) 
) t
join user
on t.user_id=user.id
order by user.id

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务