一开始没多想,看了看题目,求答题人数>=3。连表,求职,倒序结束。

注意点:要求的是每月>=3的用户,而不是全部答题>=3

第一次报错:



select tag,count(tag) tag_cnt from exam_record e1
 join
(select uid,sum(if(submit_time is not null,1,0)) cnt from exam_record
group by uid having cnt >=3) a
on e1.uid=a.uid
join examination_info e2
on e1.exam_id=e2.exam_id
group by tag
order by tag_cnt desc

因为题目还有一句,当月>=3.上边的语句没考虑这个条件。把语句的第一段子查询放到筛选条件中

通过:

select
tag,count(tag) tag_cnt
from exam_record e1
join examination_info e2
on e1.exam_id=e2.exam_id
where uid in
(select distinct uid from  (select uid,date_format(start_time,"%Y-%m"),sum(if(submit_time is not null,1,0)) cnt from exam_record
group by uid,date_format(start_time,"%Y-%m") having cnt >=3)  a)
group by tag
order by tag_cnt desc