看到题目:手下能找到的数据有哪些:
1.SQL的试卷发了几张
2.发的时间
3.做了 SQL 的那些用户
4.用户等级
5.它们所有的平均分
第 1 (2)个:
select distinct ei.exam_id, substring(ei.release_time, 1, 10)
from examination_info ei
where tag = 'SQL'
一张:9001
第三个:
select distinct ei.exam_id, substring(ei.release_time, 1, 10), er.uid
from examination_info ei
left join exam_record er on er.exam_id = ei.exam_id -- +
where tag = 'SQL'
四个人
第四个:
select distinct ei.exam_id, substring(ei.release_time, 1, 10), er.uid
from examination_info ei
left join exam_record er on er.exam_id = ei.exam_id
inner join user_info ui on er.uid = ui.uid and ui.level > 5 -- +(inner)
where tag = 'SQL'
3 个人五级以上
因为左连不行,等于五级的 1003 也匹配上去了,为了过滤改成 inner 碰掉了
又要同一天的,加上日期匹配
select distinct ei.exam_id, substring(ei.release_time, 1, 10), er.uid from examination_info ei
left join exam_record er on er.exam_id = ei.exam_id
and date_format(er.start_time, '%Y%M') = date_format(ei.release_time, '%Y%M') -- +(同一天的)
inner join user_info ui on ui.uid = er.uid and ui.level > 5
where tag = 'SQL'
第五个:
剩下就是分组
select distinct ei.exam_id,count(distinct er.uid) uv, round(avg(er.score), 1) --+avg_score from examination_info ei
left join exam_record er on er.exam_id = ei.exam_id
and date_format(er.start_time, '%Y%M') = date_format(ei.release_time, '%Y%M')
inner join user_info ui on ui.uid = er.uid and ui.level > 5
where tag = 'SQL'
group by ei.exam_id -- +
order by uv desc, avg_score asc -- +
用了函数不用另外分组 取小数位