题解 | #获得积分最多的人(三)#
获得积分最多的人(三)
https://www.nowcoder.com/practice/d2b7e2a305a7499fb310dc82a43820e8
WITH total_grade AS ( SELECT user_id, SUM(IF(type='add', grade_num, -grade_num)) AS grade FROM grade_info GROUP BY user_id ), max_grade AS ( SELECT MAX(grade) FROM total_grade ) SELECT u.id, u.name, t.grade AS grade_num FROM user AS u INNER JOIN total_grade AS t ON u.id = t.user_id WHERE t.grade IN (SELECT * FROM max_grade);