题解 | #满足条件的用户的试卷完成数和题目练习数#

满足条件的用户的试卷完成数和题目练习数

https://www.nowcoder.com/practice/5c03f761b36046649ee71f05e1ceecbf

参考的他人题解思路,个人认为很好理解
select uid,exam_cnt,if(question_cnt is null, 0, question_cnt) question_cnt
from (
    select
    uid,
    count(submit_time) as question_cnt
from practice_record
where YEAR(submit_time) = 2021
group by uid
     ) t1 right join (
         select
    uid,
    count(submit_time) as exam_cnt
from exam_record
where YEAR(submit_time) = 2021
group by uid
) t2 using (uid)
where uid in (
    select
    uid
from exam_record
join examination_info using(exam_id)
join user_info using(uid)
where tag = 'SQL' and difficulty = 'hard' and `level` = 7
group by uid
having avg(score) >= 80
    )
order by exam_cnt asc, question_cnt desc
注意其中加粗的right join,因为存在某些人有试卷完成记录,但没有答题记录,我们要找的并不是同时有试卷完成记录和答题记录的人,都没有都没有关系

#MySQL#
全部评论

相关推荐

不愿透露姓名的神秘牛友
04-08 05:32
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务