SQL38 筛选限定昵称成就值活跃日期的用户

题目主要信息:

  • 找到昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,且最近一次活跃(答题或作答试卷)在2021年9月的用户信息

问题拆分:

  • 先从表exam_record中筛选出月份是2021年9月提交的用户ID。where date_format(submit_time, '%Y%m') = '202109' 知识点:where、date_format
  • 再从表practice_record中筛选出月份是2021年9月提交的用户ID. 知识点:where、date_format
  • 从user_info表中筛选出成就值在1200到2500之间,uid在上述两个任意一个中,且nick_name能匹配牛客在首,号在结尾的情况。where nick_name like '牛客%' and nick_name like '%号' 知识点:like、where、in

代码:

select uid,
       nick_name,
       achievement
from user_info
where nick_name like '牛客%'
and nick_name like '%号'
and achievement between 1200 and 2500
and (
    uid in(
        select uid
        from exam_record
        where date_format(submit_time, '%Y%m') = '202109')
    or uid in(
        select uid
        from practice_record
        where  date_format(submit_time, '%Y%m') = '202109')
)