select ui.uid, round(avg(if(submit_time is null, 0, score)), 0) as avg_score, round(avg(if(submit_time is null, duration, timestampdiff(minute, start_time, submit_time))), 1) as avg_time_took from user_info as ui join exam_record as er on ui.uid = er.uid join examination_info as ei on ei.exam_id = er.exam_id where level = 0 and difficulty = 'hard' group by uid
1.先用uid作为桥梁连接user_info和exam_record, 再用exam_id作为桥梁连接examination_info
2.再用等级为0和难度为hard作为条件筛选数据。
3.要求输出的是每个等级为0的用户的信息,所以要按uid分组。
4.难点1是求平均分,其中未交卷的分数为0。用if语句选择要计入的分数,再用avg函数求平均分,最后用round函数保留小数位数。
5.难点2是求平均时间,其中未交卷的用时为考试时长。同样用if选择要计入的时间,再用avg函数求平均时间,最后用round函数保留1位小数。