select info.uid, count(distinct act_month) as act_month_total, count(distinct if(year(act_day)=2021,act_day,null)) as act_days_2021, count(distinct if(year(act_day)=2021 and tag = 'exam',act_day,null)) as act_days_2021_exam, count(distinct if(year(act_day)=2021 and tag = 'question',act_day,null)) as act_days_2021_question from (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_question right join user_info as info on exam_and_question.uid = info.uid where info.level>=6 group by info.uid order by act_month_total desc, act_days_2021 desc