请找到昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,且最近一次活跃(答题或作答试卷)在2021年9月的用户信息。
解释:昵称以『牛客』开头『号』结尾且成就值在1200~2500之间的有1002、1004;
1002最近一次试卷区开始作答时间2021年9月,交卷时间为2021年9月;1004最近一次试卷区开始作答时间为2021年8月,未交卷。
因此最终满足条件的只有1002。
请找到昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,且最近一次活跃(答题或作答试卷)在2021年9月的用户信息。
drop table if exists user_info,exam_record,practice_record; 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 practice_record ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int NOT NULL COMMENT '用户ID', question_id int NOT NULL COMMENT '题目ID', submit_time datetime COMMENT '提交时间', score tinyint 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号', 1000, 2, '算法', '2020-01-01 10:00:00'), (1002, '牛客2号', 1200, 3, '算法', '2020-01-01 10:00:00'), (1003, '进击的3号', 2200, 5, '算法', '2020-01-01 10:00:00'), (1004, '牛客4号', 2500, 6, '算法', '2020-01-01 10:00:00'), (1005, '牛客5号', 3000, 7, 'C++', '2020-01-01 10:00:00'); INSERT INTO practice_record(uid,question_id,submit_time,score) VALUES (1001, 8001, '2021-08-02 11:41:01', 60), (1002, 8001, '2021-09-02 19:30:01', 50), (1002, 8001, '2021-09-02 19:20:01', 70), (1002, 8002, '2021-09-02 19:38:01', 70), (1003, 8002, '2021-09-01 19:38:01', 80); 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), (1003, 9003, '2021-02-06 12:01:01', null, null), (1003, 9001, '2021-09-07 10:01:01', '2021-09-07 10:31:01', 89), (1004, 9002, '2021-08-06 12:01:01', null, null), (1002, 9001, '2020-01-01 12:01:01', '2020-01-01 12:31:01', 81), (1002, 9002, '2020-02-01 12:01:01', '2020-02-01 12:31:01', 82), (1002, 9002, '2020-02-02 12:11:01', '2020-02-02 12:31:01', 83), (1005, 9001, '2021-02-01 11:01:01', '2021-02-01 11:31:01', 84), (1006, 9001, '2021-02-01 11:01:01', '2021-02-01 11:31:01', 84), (1002, 9001, '2021-09-06 12:01:01', '2021-09-06 12:21:01', 80), (1002, 9001, '2021-09-06 12:01:01', null, null), (1002, 9001, '2021-09-07 12:01:01', null, null);
1002|牛客2号|1200
select uid ,nick_name ,achievement from user_info where uid in (select uid from exam_record er left join practice_record pr using(uid) where date_format(er.start_time,'%Y%m')=202109&nbs***bsp;date_format(pr.submit_time,'%Y%m')=202109 ) and nick_name like '牛客%号' and achievement >=1200 and achievement <=2500
with t0 as (select uid, submit_time from exam_record where date_format(submit_time,'%Y-%m')='2021-09' and score>0 union select uid, submit_time from practice_record where date_format(submit_time,'%Y-%m')='2021-09' and score>0) select uid, nick_name, achievement from t0 left join user_info using(uid) where achievement between 1200 and 2500 and nick_name like '牛客%号' group by 1不聪明,但是我觉得很清晰吧。。。。
select c.uid,c.nick_name,c.achievement from ( select a.uid as uid,a.nick_name as nick_name, a.achievement as achievement, b.start_time as times, e.submit_time as timess, ROW_NUMBER() OVER(partition by b.uid order by b.start_time DESC) as ranks, ROW_NUMBER() OVER(partition by e.uid order by e.submit_time DESC) as rankss from user_info a left join exam_record b on a.uid= b.uid left join practice_record e on a.uid= e.uid where a.nick_name like "牛客%号" and a.achievement between 1200 and 2500) as c where (c.ranks=1 and date_format(c.times,'%Y%m') = '202109') or (c.rankss=1 and date_format(c.timess,'%Y%m') = '202109') order by c.uid;
select uid, nick_name, achievement from ( select ui.uid, nick_name, achievement, max(start_time) over(partition by er.uid) last_time, max(pr.submit_time) over(partition by er.uid) last_pr from user_info ui left join exam_record er on ui.uid = er.uid left join practice_record pr on ui.uid = pr.uid where nick_name like '牛客%号' and achievement between 1200 and 2500 )t where last_time like '2021-09%'&nbs***bsp;last_pr like '2021-09%' group by uid, nick_name, achievement
SELECT uid,nick_name,achievement FROM user_info WHERE nick_name like '牛客_号' and achievement between 1200 and 2500 and uid in ( SELECT uid from ( SELECT distinct uid,max(submit_time) over (partition by uid ) as a FROM (SELECT uid,submit_time FROM exam_record UNION SELECT uid,submit_time FROM practice_record ) t1 ) t2 where date_format(a,'%Y%m') = '202109' )
SELECT DISTINCT uid,nick_name,achievement FROM (SELECT uid,exam_id,submit_time FROM exam_record AS e_r UNION ALL SELECT uid,question_id,submit_time FROM practice_record AS p_r)AS t1 JOIN user_info AS u_i USING(uid) WHERE nick_name LIKE '牛客%号' AND achievement BETWEEN 1200 AND 2500 GROUP BY uid,nick_name,achievement HAVING MAX(DATE_FORMAT(submit_time,'%Y%m')) = '202109'JOIN...USING()记得要在using的列名上加括号。
# 计算试卷区最近活跃时间 with t1 as ( select distinct uid, max(date_format(start_time,"%Y%m")) over(partition by uid) as start_month from exam_record ) # 计算题目区最近活跃时间 , t2 as ( select distinct uid, max(date_format(submit_time,"%Y%m")) over(partition by uid) as submit_month from practice_record ) # 两张表合并 计算出每个用户的试卷区和题目区最近活跃时间 ,t3 as( select t1.uid, start_month, submit_month from t1 left join t2 on t1.uid =t2.uid ) # 通过比较试卷区和题目区最近的活跃时间,求出最近的活跃时间 ,t4 as ( select uid, (case when start_month>=submit_month then start_month when start_month<submit_month then submit_month when start_month is null and submit_month is not null then submit_month else start_month end ) as recent_month from t3 ) # 筛选满足条件的字段 select t4.uid, nick_name, achievement from t4,user_info where t4.uid=user_info.uid and nick_name like '牛客%号' and achievement between 1200 and 2500 and recent_month=202109
select distinct uid,nick_name,achievement from ( select a.uid,nick_name,achievement,month,row_number()over(partition by a.uid order by month desc) rk from ( select uid,date_format(start_time,'%Y%m') month from exam_record e union all select uid,date_format(submit_time,'%Y%m') month from practice_record p )a left join user_info u on a.uid=u.uid where nick_name like '牛客%号' and achievement between 1200 and 2500 )b where month=202109 and rk=1
select uid ,nick_name ,achievement from ( select uid ,nick_name ,achievement ,time_time ,row_number() over(partition by uid order by time_time desc) ranking from ( select ui.uid ,ui.nick_name ,ui.achievement ,er.start_time time_time from user_info ui join exam_record er on ui.uid=er.uid union all select ui.uid ,ui.nick_name ,ui.achievement ,pr.submit_time time_time from user_info ui join practice_record pr on ui.uid=pr.uid ) neww where nick_name like "牛客%号" and achievement >=1200 and achievement <=2500 ) new_new where ranking=1 and substr(time_time,1,7)='2021-09'
select uid, nick_name, achievement from ( select uid, nick_name, achievement, row_number() over ( partition by uid order by start_time desc ) as rank1 from practice_record inner join exam_record using (uid) inner join user_info using (uid) where nick_name like "牛客%%号" and achievement between 1200 and 2500 and date_format (start_time, '%Y%m') = 202109 ) as t1 where rank1<=1 不理解怎么通不过了呢