思路,分成两个部分,union all 一个表,再join一个表
select f.uid,f.act_month_total,f.act_days_2021,g.act_days_2021_exam,g.act_days_2021_question
from
(select a.uid,COUNT(DISTINCT DATE_FORMAT(time,"%y-%m"))as act_month_total,
COUNT(DISTINCT case when year(time)=2021 then DATE_FORMAT(time,"%y-%m-%d") else null end)as act_days_2021
from (select uid from user_info where `level`=6 or `level`=7)a
left join
(select e.uid,e.start_time as time from exam_record e
union all
select p.uid,p.submit_time as time from practice_record p
) c
on a.uid=c.uid
group by a.uid)f
left JOIN
(select d.uid,count(distinct case when year(e1.start_time)=2021 then date(e1.start_time) else null end) as act_days_2021_exam,count(distinct case when year(p1.submit_time)=2021 then date(p1.submit_time) else null end)as act_days_2021_question
from(SELECT uid from user_info where `level`=6 or `level`=7)d
left join
exam_record e1
on d.uid=e1.uid
left join practice_record p1
on d.uid=p1.uid
group by d.uid)g
on f.uid=g.uid
order by
act_month_total desc ,act_days_2021 desc