SQL152. 注册当天就完成了试卷的名单第三页
描述
现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):id | uid | nick_name | achievement | level | job | register_time |
1 | 1001 | 牛客1 | 19 | 0 | 算法 | 2020-01-01 10:00:00 |
2 | 1002 | 牛客2号 | 1200 | 3 | 算法 | 2020-01-01 10:00:00 |
3 | 1003 | 牛客3号♂ | 22 | 0 | 算法 | 2020-01-01 10:00:00 |
4 | 1004 | 牛客4号 | 25 | 0 | 算法 | 2020-01-01 10:00:00 |
5 | 1005 | 牛客555号 | 4000 | 7 | 算法 | 2020-01-11 10:00:00 |
6 | 1006 | 牛客6号 | 25 | 0 | 算法 | 2020-01-02 11:00:00 |
7 | 1007 | 牛客7号 | 25 | 0 | 算法 | 2020-01-02 11:00:00 |
8 | 1008 | 牛客8号 | 25 | 0 | 算法 | 2020-01-02 11:00:00 |
9 | 1009 | 牛客9号 | 25 | 0 | 算法 | 2020-01-02 11:00:00 |
10 | 1010 | 牛客10号 | 25 | 0 | 算法 | 2020-01-02 11:00:00 |
11 | 1011 | 666666 | 3000 | 6 | C++ | 2020-01-02 10:00:00 |
id | exam_id | tag | difficulty | duration | release_time |
1 | 9001 | 算法 | hard | 60 | 2020-01-01 10:00:00 |
2 | 9002 | 算法 | hard | 80 | 2020-01-01 10:00:00 |
3 | 9003 | SQL | medium | 70 | 2020-01-01 10:00:00 |
id | uid | exam_id | start_time | submit_time | score |
1 | 1001 | 9001 | 2020-01-02 09:01:01 | 2020-01-02 09:21:59 | 80 |
2 | 1002 | 9003 | 2020-01-20 10:01:01 | 2020-01-20 10:10:01 | 81 |
3 | 1002 | 9002 | 2020-01-01 12:11:01 | 2020-01-01 12:31:01 | 83 |
4 | 1003 | 9002 | 2020-01-01 19:01:01 | 2020-01-01 19:30:01 | 75 |
5 | 1004 | 9002 | 2020-01-01 12:01:01 | 2020-01-01 12:11:01 | 60 |
6 | 1005 | 9002 | 2020-01-01 12:01:01 | 2020-01-01 12:41:01 | 90 |
7 | 1006 | 9001 | 2020-01-02 19:01:01 | 2020-01-02 19:32:00 | 20 |
8 | 1007 | 9002 | 2020-01-02 19:01:01 | 2020-01-02 19:40:01 | 89 |
9 | 1008 | 9003 | 2020-01-02 12:01:01 | 2020-01-02 12:20:01 | 99 |
10 | 1008 | 9001 | 2020-01-02 12:01:01 | 2020-01-02 12:31:01 | 98 |
11 | 1009 | 9002 | 2020-01-02 12:01:01 | 2020-01-02 12:31:01 | 82 |
12 | 1010 | 9002 | 2020-01-02 12:11:01 | 2020-01-02 12:41:01 | 76 |
13 | 1011 | 9001 | 2020-01-02 10:01:01 | 2020-01-02 10:31:01 | 89 |
找到求职方向为算法工程师,且注册当天就完成了算法类试卷的人,按参加过的所有考试最高得分排名。排名榜很长,我们将采用分页展示,每页3条,现在需要你取出第3页(页码从1开始)的人的信息。
uid | level | register_time | max_score |
1010 | 0 | 2020-01-02 11:00:00 | 76 |
1003 | 0 | 2020-01-01 10:00:00 | 75 |
1004 | 0 | 2020-01-01 11:00:00 | 60 |
uid | level | register_time | max_score |
1008 | 0 | 2020-01-02 11:00:00 | 99 |
1005 | 7 | 2020-01-01 10:00:00 | 90 |
1007 | 0 | 2020-01-02 11:00:00 | 89 |
1002 | 3 | 2020-01-01 10:00:00 | 83 |
1009 | 0 | 2020-01-02 11:00:00 | 82 |
1001 | 0 | 2020-01-01 10:00:00 | 80 |
1010 | 0 | 2020-01-02 11:00:00 | 76 |
1003 | 0 | 2020-01-01 10:00:00 | 75 |
1004 | 0 | 2020-01-01 11:00:00 | 60 |
1006 | 0 | 2020-01-02 11:00:00 | 20 |
示例1
输入:
drop table if exists examination_info,user_info,exam_record; CREATE TABLE examination_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', exam_id int UNIQUE NOT NULL COMMENT '试卷ID', tag varchar(32) COMMENT '类别标签', difficulty varchar(8) COMMENT '难度', duration int NOT NULL COMMENT '时长', release_time datetime COMMENT '发布时间' )CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE TABLE user_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int UNIQUE NOT NULL COMMENT '用户ID', `nick_name` varchar(64) COMMENT '昵称', achievement int COMMENT '成就值', level int COMMENT '用户等级', job varchar(32) COMMENT '职业方向', register_time datetime COMMENT '注册时间' )CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE TABLE exam_record ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int NOT NULL COMMENT '用户ID', exam_id int NOT NULL COMMENT '试卷ID', start_time datetime NOT NULL COMMENT '开始时间', submit_time datetime COMMENT '提交时间', score tinyint COMMENT '得分' )CHARACTER SET utf8 COLLATE utf8_general_ci; INSERT INTO user_info(uid,`nick_name`,achievement,`level`,job,register_time) VALUES (1001, '牛客1', 19, 0, '算法', '2020-01-01 10:00:00'), (1002, '牛客2号', 1200, 3, '算法', '2020-01-01 10:00:00'), (1003, '牛客3号♂', 22, 0, '算法', '2020-01-01 10:00:00'), (1004, '牛客4号', 25, 0, '算法', '2020-01-01 11:00:00'), (1005, '牛客555号', 4000, 7, '算法', '2020-01-01 10:00:00'), (1006, '牛客6号', 25, 0, '算法', '2020-01-02 11:00:00'), (1007, '牛客7号', 25, 0, '算法', '2020-01-02 11:00:00'), (1008, '牛客8号', 25, 0, '算法', '2020-01-02 11:00:00'), (1009, '牛客9号', 25, 0, '算法', '2020-01-02 11:00:00'), (1010, '牛客10号', 25, 0, '算法', '2020-01-02 11:00:00'), (1011, '666666', 3000, 6, 'C++', '2020-01-02 10:00:00'); INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES (9001, '算法', 'hard', 60, '2020-01-01 10:00:00'), (9002, '算法', 'hard', 80, '2020-01-01 10:00:00'), (9003, 'SQL', 'medium', 70, '2020-01-01 10:00:00'); INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES (1001, 9001, '2020-01-01 09:01:01', '2020-01-01 09:21:59', 80), (1002, 9003, '2020-01-20 10:01:01', '2020-01-20 10:10:01', 81), (1002, 9002, '2020-01-01 12:11:01', '2020-01-01 12:31:01', 83), (1003, 9002, '2020-01-01 19:01:01', '2020-01-01 19:30:01', 75), (1004, 9002, '2020-01-01 12:01:01', '2020-01-01 12:11:01', 60), (1005, 9002, '2020-01-01 12:01:01', '2020-01-01 12:41:01', 90), (1006, 9001, '2020-01-02 19:01:01', '2020-01-02 19:32:00', 20), (1007, 9002, '2020-01-02 19:01:01', '2020-01-02 19:40:01', 89), (1008, 9003, '2020-01-02 12:01:01', '2020-01-02 12:20:01', 99), (1008, 9001, '2020-01-02 12:01:01', '2020-01-02 12:31:01', 98), (1009, 9002, '2020-01-02 12:01:01', '2020-01-02 12:31:01', 82), (1010, 9002, '2020-01-02 12:11:01', '2020-01-02 12:41:01', 76), (1011, 9001, '2020-01-02 10:01:01', '2020-01-02 10:31:01', 89);
输出:
1010|0|2020-01-02 11:00:00|76 1003|0|2020-01-01 10:00:00|75 1004|0|2020-01-01 11:00:00|60
Mysql 解法, 执行用时: 36ms, 内存消耗: 6412KB, 提交时间: 2022-01-03
select a.uid,b.level,b.register_time,max(a.score) as max_score from exam_record a inner join user_info b on a.uid=b.uid inner join examination_info c on a.exam_id=c.exam_id GROUP by a.uid,b.level,b.register_time having a.uid in(select a.uid from exam_record a inner join user_info b on a.uid=b.uid inner join examination_info c on a.exam_id=c.exam_id where b.job='算法' and c.tag='算法' and DATE_FORMAT(a.submit_time,'%Y%m%d')=DATE_FORMAT(b.register_time,'%Y%m%d')) order by max_score desc limit 6,3
Mysql 解法, 执行用时: 37ms, 内存消耗: 6384KB, 提交时间: 2021-12-17
# register and take the test # select distinct er.uid # from exam_record er, user_info ui, examination_info ei # where er.uid = ui.uid and er.exam_id = ei.exam_id and tag = '算法' # and date_format(register_time,'%Y%m') = date_format(submit_time,'%Y%m') # select er.uid, level, register_time,max(score) max_score # from exam_record er, user_info ui, examination_info ei # where er.uid = ui.uid and er.exam_id = ei.exam_id # and er.uid in # (select distinct er.uid # from exam_record er, user_info ui, examination_info ei # where er.uid = ui.uid and er.exam_id = ei.exam_id and tag = '算法' and job = '算法' # and date_format(register_time,'%Y%m') = date_format(submit_time,'%Y%m') # ) # group by er.uid, level, register_time # order by max_score desc # limit 6,3 with t as ( select t.uid,level,register_time,start_time,score,tag from user_info t join examination_info t1 join exam_record t2 on t.uid = t2.uid and t1.exam_id = t2.exam_id where job = '算法' ) select uid,level,register_time,max_score from (select *,dense_rank()over(order by max_score desc) as rn from (select uid,level,register_time,max(score) as max_score from t WHERE uid in (select uid from t where datediff(date(register_time),date(start_time))=0 and tag='算法') group by uid) t1) t2 where rn BETWEEN 7 and 9 and max_score is not null
Mysql 解法, 执行用时: 37ms, 内存消耗: 6392KB, 提交时间: 2022-01-27
select er.uid,ui.level,ui.register_time,max(er.score) as max_score from exam_record er inner join examination_info ei on er.exam_id=ei.exam_id inner join user_info ui on er.uid=ui.uid where ui.job='算法' and ei.tag='算法' and date(ui.register_time)=date(er.submit_time) group by er.uid order by max_score desc limit 6,3;
Mysql 解法, 执行用时: 37ms, 内存消耗: 6396KB, 提交时间: 2022-01-24
SELECT DISTINCT uid, level, register_time, MAX(score) OVER(PARTITION BY uid) AS max_score FROM exam_record JOIN user_info USING(uid) WHERE uid IN( SELECT uid FROM exam_record JOIN user_info USING(uid) JOIN examination_info USING(exam_id) WHERE job = '算法'#1.求职方向为算法工程师 AND (DATEDIFF(register_time,submit_time)=0 AND tag = '算法')#2.注册当天就完成了算法类试卷 ) ORDER BY max_score DESC#3.按参加过的所有考试最高得分排名 LIMIT 6,3#4.返回第7~9条
Mysql 解法, 执行用时: 37ms, 内存消耗: 6396KB, 提交时间: 2021-12-13
select a.uid,level,register_time,max(score) over(partition by a.uid) as max_score from user_info a left join exam_record b on a.uid=b.uid left join examination_info c on b.exam_id=c.exam_id where job='算法' and substr(register_time,1,7)=substr(submit_time,1,7) and tag='算法' order by max_score desc limit 6,3