题解 | #筛选限定昵称成就值活跃日期的用户#
筛选限定昵称成就值活跃日期的用户
http://www.nowcoder.com/practice/2ed07ff8f67a474d90523b88402e401b
select ui.uid,nick_name,achievement from user_info ui #用info表去左连可以避免有的用户只考试没练习或只练习没考试的情况 left join( select uid,max(month(start_time)) as exam_act from exam_record where year(start_time)='2021' group by uid )t1 #每个用户考试的最近活跃时间 on ui.uid=t1.uid left join( select uid,max(month(submit_time)) as practice_act from practice_record where year(submit_time)='2021' group by uid )t2 #每个用户练习的最近活跃时间 on ui.uid=t2.uid where nick_name like "牛客%号" and achievement between 1200 and 2500 and (exam_act=9 or practice_act=9)
分下面几个步骤:
1.取出考试表中每个用户对应的2021年最近活跃时间:max(month(start_time))
2.取出练习表中每个用户对应的2021年最近活跃时间:max(month(submit_time))
注意要限定年份为2021
3.用user_info表作为主表去左连,可以避免用户只在考试或练习活跃而没被统计到的情况
4.前3个步骤就能得到一张有user信息以及对应的考试和练习的最近活跃时间,然后根据题设条件进行where筛选即可