题解 | #每个6/7级用户活跃情况#

每个6/7级用户活跃情况

http://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1

拆解见代码块

查找的四个结果分别放在一个子表(4个子表)与用户表进行连接

重点是试卷作答和题目练习两个表的进行合并查询

以及ifnull()函数的使用

#条件:6/7级用户
#查询结果:用户总活跃月份数、
#2021年活跃天数、2021年试卷作答活跃天数、2021年答题活跃天数
#排序:照总活跃月份数、2021年活跃天数降序排序

select u.uid,ifnull(act_month_total,0),ifnull(act_days_2021,0), ifnull(act_days_2021_exam ,0),ifnull(act_days_2021_question,0)
 from user_info u
 left join (select uid,count(distinct concat(year(act_time),month(act_time))) as act_month_total
from (select uid as uid ,start_time as act_time from exam_record
union all
select uid as uid,submit_time as act_time from practice_record) b
group by uid) b1
on u.uid=b1.uid
left join (select uid,count(distinct concat(month(act_time),day(act_time))) as act_days_2021
from (select uid as uid ,start_time as act_time from exam_record
where year(start_time)='2021'
union all
select uid as uid,submit_time as act_time from practice_record
where year(submit_time)='2021' ) b
group by uid) b2
on u.uid=b2.uid
left join (select uid, count(distinct concat(month(submit_time),day(submit_time) )) as act_days_2021_exam
 from exam_record
 where year(submit_time)='2021'
 group by uid) b3
 on u.uid=b3.uid
 left join ( select uid, count(distinct concat(month(submit_time),day(submit_time) )) as act_days_2021_question
 from practice_record
 where year(submit_time)='2021'
 group by uid) b4
 on u.uid=b4.uid
 where level =6 or level =7
order by act_month_total desc , act_days_2021 desc 

在尝试其他方法的时候遇到这种情况,有大佬帮忙解答一下吗?

alt 这是外连接的样子 alt 1002,1003,1005明明是空的为什么用count的时候会被计上数?结果显示为1??

全部评论

相关推荐

双非一本失业第二年:《机器视觉垃圾分类》
点赞 评论 收藏
分享
ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
评论
点赞
1
分享
牛客网
牛客企业服务