现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间),数据如下: id uid nick_name achievement level job register_time 1 1001 牛客1号 10 0 算法 2020-01-01 10:00:00 2 1002 牛客2号 2100 6 算法 2020-01-01 10:00:00 试卷信息表examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间),数据如下: id exam_id tag difficulty duration release_time 1 9001 SQL hard 60 2020-01-01 10:00:00 2 9002 SQL easy 60 2020-01-01 10:00:00 3 9004 算法 medium 80 2020-01-01 10:00:00 试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分),数据如下: 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 1001 9001 2021-05-02 10:01:01 (NULL) (NULL) 3 1001 9002 2021-02-02 19:01:01 2021-02-02 19:30:01 87 4 1001 9001 2021-06-02 19:01:01 2021-06-02 19:32:00 20 5 1001 9002 2021-09-05 19:01:01 2021-09-05 19:40:01 89 6 1001 9002 2021-09-01 12:01:01 (NULL) (NULL) 7 1002 9002 2021-05-05 18:01:01 2021-05-05 18:59:02 90 请输出每个0级用户所有的高难度试卷考试平均用时和平均得分,未完成的默认试卷最大考试时长和0分处理。由示例数据结果输出如下: uid avg_score avg_time_took 1001 33 36.7 解释:0级用户有1001,高难度试卷有9001,1001作答9001的记录有3条,分别用时20分钟、未完成(试卷时长60分钟)、30分钟(未满31分钟),分别得分为80分、未完成(0分处理)、20分。因此他的平均用时为1103=36.7(保留一位小数),平均得分为33分(取整)
示例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号', 10, 0, '算法', '2020-01-01 10:00:00'),
(1002, '牛客2号', 2100, 6, '算法', '2020-01-01 10:00:00');
INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
(9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'),
(9002, 'SQL', 'easy', 60, '2020-01-01 10:00:00'),
(9004, '算法', 'medium', 80, '2020-01-01 10:00:00');
INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2020-01-02 09:01:01', '2020-01-02 09:21:59', 80),
(1001, 9001, '2021-05-02 10:01:01', null, null),
(1001, 9002, '2021-02-02 19:01:01', '2021-02-02 19:30:01', 87),
(1001, 9001, '2021-06-02 19:01:01', '2021-06-02 19:32:00', 20),
(1001, 9002, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 89),
(1001, 9002, '2021-09-01 12:01:01', null, null),
(1002, 9002, '2021-05-05 18:01:01', '2021-05-05 18:59:02', 90);
加载中...