这道题把逻辑理顺就很好做了。
首先是将三张表连接起来,examination_info和exam_record根据exam_id还有release_time和start_time的配对来进行连接。这里要注意的是只需要对两个时间的日期进行匹配。exam_record和user_info根据uid连接。
然后用where限制level>5并根据exam_id进行分组。选出exam_id并用count和avg算出每组作答人数和平均分。最后order by根据作答人数降序排列,根据平均分升序排列。
select er.exam_id, count(distinct er.uid) as uv, round(avg(er.score),1) as avg_score
from
examination_info as ei
inner join
exam_record as er
on ei.exam_id = er.exam_id and
date_format(ei.release_time, "%Y-%m%d")=date_format(er.start_time, "%Y-%m%d")
inner join user_info ui
on ui.uid = er.uid
where ui.level > 5
group by er.exam_id
order by uv desc, avg_score