首页 > 试题广场 >

筛选限定昵称成就值活跃日期的用户

[编程题]筛选限定昵称成就值活跃日期的用户
  • 热度指数:42496 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):

试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):

题目练习记录表practice_record(uid用户ID, question_id题目ID, submit_time提交时间, score得分):

请找到昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,且最近一次活跃(答题或作答试卷)在2021年9月的用户信息。

由示例数据结果输出如下:

解释:昵称以『牛客』开头『号』结尾且成就值在1200~2500之间的有1002、1004;
1002最近一次试卷区开始作答时间2021年9月,交卷时间为2021年9月;1004最近一次试卷区开始作答时间为2021年8月,未交卷。
因此最终满足条件的只有1002。
示例1

输入

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

发表于 2025-05-06 19:33:34 回复(0)
SELECT uid, nick_name, achievement
FROM(
    SELECT uid, nick_name, achievement
    FROM user_info
    WHERE nick_name LIKE '牛客%号' AND achievement BETWEEN 1200 AND 2500
)a JOIN (
    SELECT uid, DATE_FORMAT(MAX(start_time),'%Y%m') AS month
    FROM exam_record
    GROUP BY uid
    UNION
    SELECT uid, DATE_FORMAT(MAX(submit_time),'%Y%m') AS month
    FROM practice_record
    GROUP BY uid
)b USING(uid)
WHERE month = '202109';
发表于 2025-04-06 15:05:41 回复(0)
/*昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,且最近一次活跃(答题或作答试卷)在2021年9月的用户信息。
1、昵称以『牛客』开头『号』结尾
2、成就值在1200~2500之间
3、最近一次活跃(答题或作答试卷)在2021年9月

*/

select a.uid,a.nick_name,a.achievement from user_info a
left join (select uid from exam_record group by uid
having   date_format(max(start_time),'%Y%m')='202109'
) b on a.uid=b.uid
left join (select uid from practice_record group by uid
having   date_format(max(submit_time),'%Y%m')='202109') c on a.uid=c.uid
where  nick_name like '牛客%号' and achievement between 1200 and 2500
and (b.uid is not null or c.uid is not null)
发表于 2024-12-19 15:34:44 回复(0)
自己测试是对的,输进去就不对了是为什么啊
SELECT DISTINCT
    uid,
    nick_name,
    achievement
FROM user_info
LEFT JOIN exam_record USING(uid)
LEFT JOIN practice_record USING(uid)
WHERE nick_name REGEXP '^牛客.*号$'
AND achievement BETWEEN 1200 AND 2500
AND uid IN (
SELECT uid
FROM exam_record
GROUP BY uid
HAVING MAX(start_time) LIKE '2021-09%'
UNION ALL
SELECT uid
FROM exam_record
GROUP BY uid
HAVING MAX(start_time) LIKE '2021-09%'
ORDER BY uid
);
发表于 2024-11-24 16:14:32 回复(0)
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
不聪明,但是我觉得很清晰吧。。。。
发表于 2024-10-15 18:06:27 回复(0)
select
p.uid,nick_name,achievement
from user_info p
left join (
   select
    uid
    ,t
    ,dense_rank()over(partition by uid order by t desc) rn
    from
    (
    select
    uid,start_time t
    from exam_record
    union all
    select
    uid,submit_time t
    from practice_record
    )u    
)d
on p.uid=d.uid
where rn=1
and achievement between 1200 and 2500
and nick_name regexp '^牛客.*号$'
and year(t)=2021 and month(t)=9
;

发表于 2024-10-02 21:51:48 回复(0)
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;

发表于 2024-10-01 18:11:40 回复(0)
注意:左连

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

发表于 2024-07-10 09:44:15 回复(0)
select er.uid,ui.nick_name,ui.achievement
from exam_record er join
(select * from user_info where nick_name like '牛客%号'
and achievement >=1200 and achievement<=2500) ui
on er.uid=ui.uid
left join practice_record as pr on er.uid=pr.uid
group by er.uid
having date_format(max(er.start_time),'%Y%m')=202109
or date_format(max(pr.submit_time),'%Y%m')=202109
发表于 2024-05-22 14:48:09 回复(0)
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'
)

发表于 2024-04-01 08:45:52 回复(0)
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 uid,start_time
        from exam_record
        union all
        select uid,submit_time as start_time
        from practice_record
    )t1
    group by uid
    having max(date_format(start_time,'%Y%m')) = '202109'
)
发表于 2024-03-26 19:31:43 回复(0)
select
    u.uid,
    u.nick_name,
    u.achievement
from
(select
    uid,
    nick_name,
    achievement
from
    user_info
where
    nick_name like '牛客%号'
    and achievement between 1200 and 2500) u
    left join exam_record er on u.uid = er.uid
    left join practice_record pr on u.uid = pr.uid
where date_format(er.start_time,"%Y%m") = 202109
    or date_format(pr.submit_time,"%Y%m") = 202109
group by
    u.uid,
    u.nick_name,
    u.achievement
having
    count(er.id) + count(pr.id) > 0
发表于 2024-03-25 18:15:43 回复(0)
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的列名上加括号。
发表于 2024-03-22 12:00:58 回复(0)
# 计算试卷区最近活跃时间
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
发表于 2023-12-18 15:17:31 回复(0)
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

1.最近一次活跃包括试卷区和题目区活跃,所以先将两个表union起来
2.再union后的表left join用户信息,按条件筛选满足条件的用户,并对每个用户的活跃时间进行排序,得到最后活跃时间排序为1
3.输出想要的结果
发表于 2023-11-29 15:07:01 回复(0)
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'

发表于 2023-11-28 16:01:29 回复(0)
为什么是leftjoin 这几个join我感觉我就有点随缘用
发表于 2023-10-10 10:33:02 回复(0)
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
不理解怎么通不过了呢

发表于 2023-09-15 16:50:17 回复(0)