sql多表查询之连接查询

题1

https://www.nowcoder.com/practice/5c03f761b36046649ee71f05e1ceecbf?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0

select 
    uid,
    exam_cnt,
    if(question_cnt is null, 0, question_cnt)
from
    (select 
        uid,
        count(submit_time) as exam_cnt
    from exam_record
    where YEAR(submit_time) = 2021
    group by uid) as t

left join

(select
    uid,
    count(submit_time) as question_cnt
from practice_record
where YEAR(submit_time) = 2021
group by uid) as 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

题目2

https://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0

select 
    u_i.uid as uid,
       count(distinct act_month) as act_month_total,
       count(distinct case 
             when year(act_time) = 2021 
             then act_day 
             end) as act_days_2021,
       count(distinct case 
             when year(act_time) = 2021 
             and tag = 'exam' 
             then act_day 
             end) as act_days_2021_exam,
        count(distinct case
             when year(act_time) = 2021
             and tag = 'question'
             then act_day
             end) as act_days_2021_question
from 
    user_info as u_i
    left join (select uid,
                 start_time as act_time,
                 date_format(start_time, '%Y%m') as act_month,
                 date_format(start_time, '%Y%m%d') as act_day,
                 'exam' as tag
          from exam_record
          union all 
          select uid,
                 submit_time as act_time,
                 date_format(submit_time, '%Y%m') as act_month,
                 date_format(submit_time, '%Y%m%d') as act_day,
                 'question' as tag
          from  practice_record
          ) as exam_and_practice
    on exam_and_practice.uid = u_i.uid
    
where u_i.level >= 6
group by uid
order by act_month_total desc, act_days_2021 desc


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值