首页 > 试题广场 >

短视频直播间晚上11-12点之间各直播间的在线人数

[编程题]短视频直播间晚上11-12点之间各直播间的在线人数
  • 热度指数:9791 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现有某天短视频直播间用户观看直播间的信息表user_view_tb如下,
(其中字段包含用户id:user_id、直播间id:room_id、  进入时间:in_time,离开时间:out_time)
user_id room_id in_time out_time
1 1001 10:00:00 10:30:00
2 1001 10:01:00 10:05:00
3 1001 10:05:00 10:20:00
1 1002 19:05:00 20:05:00
2 1002 19:15:00 19:55:00
2 1002 20:15:00 20:45:00
3 1002 20:15:00 20:45:00
4 1003 22:15:00 23:15:00
1 1002 23:15:00 23:45:00
4 1002 23:10:00 23:25:00
3 1002 23:00:00 23:35:00
4 1001 23:10:00 23:25:00
3 1001 23:00:00 23:35:00
4 1003 23:10:00 23:15:00
1 1001 20:10:00 20:15:00
1 1001 20:00:00 23:35:00
有直播间信息表room_info_tb如下:
room_id room_name room_type
1001 娱乐大王牌 娱乐
1002 声家班 搞笑
1003 嗨嗨嗨 搞笑
请你统计晚上11-12点之间各直播间的在线人数(包含边界值11:00、12:00),并按在线人数降序排序,以上例子输出结果如下:
room_id room_name user_count
1001 娱乐大王牌 3
1002 声家班 3
1003 嗨嗨嗨 1


示例1

输入

drop table if exists user_view_tb;
CREATE TABLE user_view_tb(
user_id int(10) NOT NULL,
room_id int(10) NOT NULL,
in_time time NOT NULL,
out_time time NOT NULL
);
INSERT INTO user_view_tb VALUES(1, 1001, '10:00:00', '10:30:00');
INSERT INTO user_view_tb VALUES(2, 1001, '10:01:00', '10:05:00');
INSERT INTO user_view_tb VALUES(3, 1001, '10:05:00', '10:20:00');
INSERT INTO user_view_tb VALUES(1, 1002, '19:05:00', '20:05:00');
INSERT INTO user_view_tb VALUES(2, 1002, '19:15:00', '19:55:00');
INSERT INTO user_view_tb VALUES(2, 1002, '20:15:00', '20:45:00');
INSERT INTO user_view_tb VALUES(3, 1002, '20:15:00', '20:45:00');
INSERT INTO user_view_tb VALUES(4, 1003, '22:15:00', '23:15:00');
INSERT INTO user_view_tb VALUES(1, 1002, '23:15:00', '23:45:00');
INSERT INTO user_view_tb VALUES(4, 1002, '23:10:00', '23:25:00');
INSERT INTO user_view_tb VALUES(3, 1002, '23:00:00', '23:35:00');
INSERT INTO user_view_tb VALUES(4, 1001, '23:10:00', '23:25:00');
INSERT INTO user_view_tb VALUES(3, 1001, '23:00:00', '23:35:00');
INSERT INTO user_view_tb VALUES(4, 1003, '23:10:00', '23:15:00');
INSERT INTO user_view_tb VALUES(1, 1001, '20:10:00', '20:15:00');
INSERT INTO user_view_tb VALUES(1, 1001, '20:00:00', '23:35:00');

drop table if exists room_info_tb;
CREATE TABLE room_info_tb(
room_id int(10) NOT NULL,
room_name varchar(20) NOT NULL,
room_type varchar(20) NOT NULL
);
INSERT INTO room_info_tb VALUES(1001, '娱乐大王牌', '娱乐');
INSERT INTO room_info_tb VALUES(1002, '声家班', '搞笑');
INSERT INTO room_info_tb VALUES(1003, '嗨嗨嗨', '搞笑');

输出

1001|娱乐大王牌|3
1002|声家班|3
1003|嗨嗨嗨|1
我觉得很多答案都有问题,因为有些人会在23点以前进入,但是23-24中间退出,有些人会在23-24之间进入,但是不会退出,还有一些人会很早就进入,但是一直不退出。
select
    r.room_id,
    room_name,
    count(distinct (u.user_id)) as user_count
from
    user_view_tb u
    left join room_info_tb r on u.room_id = r.room_id
where
    (u.in_time between "23:00:00" and "24:00:00")
         or (u.out_time between "23:00:00" and "24:00:00")
         or (u.in_time <"23:00:00" and u.out_time >"24:00:00")
group by
    r.room_id,
    room_name
order by
    user_count desc



发表于 2025-04-02 10:32:04 回复(4)
SELECT 
    r.room_id,
    r.room_name,
    COUNT(DISTINCT user_id) AS user_count
FROM user_view_tb u
JOIN room_info_tb r USING(room_id)
WHERE u.in_time <= '23:59:59' AND u.out_time >= '23:00:00'
GROUP BY r.room_id,r.room_name
ORDER BY user_count DESC

发表于 2024-12-25 09:55:37 回复(2)
select room_id, room_name, count(distinct user_id) as user_count
from user_view_tb 
left outer join room_info_tb using(room_id)
where out_time between time('23:00:00') and time('24:00:00')
group by room_id, room_name
order by count(distinct user_id) desc


发表于 2025-02-24 16:25:58 回复(0)
with t1 as (
    select distinct user_id, room_id
    from user_view_tb
    where out_time <='24:00:00' and out_time>='23:00:00'
)
,
t2 as(
    select t1.room_id, count(distinct t1.user_id) as user_count
    from t1 
    group by t1.room_id
)

select  t2.room_id, rit.room_name, t2.user_count
from t2 
join room_info_tb rit 
on t2.room_id=rit.room_id
order by t2.user_count desc

发表于 2024-07-26 20:53:20 回复(0)
#一下子理解不了,就先把所有的列出来了

SELECT
r.room_id,
r.room_name,
COUNT(DISTINCT user_id) AS user_count
FROM user_view_tb u
JOIN room_info_tb r USING (room_id)
WHERE (u.in_time <= '23:00:00' AND u.out_time BETWEEN '23:00:00' and '24:00:00')

or (u.in_time <= '23:00:00' AND u.out_time >= '24:00:00')

or (u.in_time BETWEEN '23:00:00' and '24:00:00' AND u.out_time >= '24:00:00')

or (u.in_time BETWEEN '23:00:00' and '24:00:00' AND u.out_time BETWEEN '23:00:00' and '24:00:00')

GROUP BY r.room_id,r.room_name
ORDER BY user_count DESC
发表于 今天 10:39:31 回复(2)
思路:
1、各直播间,说明需要在每个不同的直播间内统计在线人数,需要使用group by直播间
2、时间段为23:00-23:59:59,在线就需要在线时间段与23:00-23:59:59存在交集。满足进入时间小于结束时间且退出时间大于开始时间即为交集,因为进入时间大于结束时间或者退出时间小于结束时间,一定不存在交集。可以直接用时间字符串进行比较操作符判断时间大小,时间字符串会被识别为时间类型
select
    room_id,
    room_name,
    user_count
from(
    select
        rit.room_id,
        rit.room_name,
        #注意,左连接的逻辑是一段在线时间就匹配一次,所以一行实际上是一段在线时间
        #但是还需要考虑同一个用户同一直播间进进出出多次的情况
        count(distinct uvt.user_id)as user_count
    from room_info_tb rit
    left join user_view_tb uvt
        on rit.room_id=uvt.room_id
    where uvt.out_time>='23:00:00'
        and uvt.in_time<'24:00:00'
    group by rit.room_id,rit.room_name
)temp
order by user_count desc,room_id asc


发表于 2025-06-22 09:40:54 回复(0)
select r.room_id,
       r.room_name,
     count(distinct u.user_id) as user_count

from user_view_tb as u
left join room_info_tb as r
on u.room_id=r.room_id

where (u.in_time between '23:00:00' and '24:00:00' )
   &nbs***bsp;u.out_time between '23:00:00' and '24:00:00' 
group by r.room_id,r.room_name
order by user_count desc

发表于 2025-06-16 16:52:39 回复(0)
#1. 晚上11-12点
select u.room_id,r.room_name,count(distinct user_id) user_count
from user_view_tb u join room_info_tb r
on u.room_id = r.room_id
where
(out_time >= '23:00:00' and in_time<="23:00:00")
or (in_time<="24:00:00" and out_time >= '24:00:00')
or (in_time<="24:00:00" and out_time >= '23:00:00')
group by u.room_id,r.room_name
order by user_count desc
发表于 2025-05-23 15:37:51 回复(0)
select
    room_id
    , room_name
    , count(distinct user_id) as user_count
from
    (select
        ut.user_id
        , ut.room_id
        , rt.room_name
    from
        user_view_tb as ut
        left join room_info_tb as rt on ut.room_id = rt.room_id
    where
        out_time >= '23:00:00'
        and out_time <= '24:00:00'
    ) as t1
group by
    room_id
    , room_name
order by
    room_id
    , user_count desc

发表于 2025-05-22 15:57:14 回复(0)
select
 u.room_id, 
 room_name,
 count(distinct user_id) user_count
from user_view_tb u 
join room_info_tb r on u.room_id = r.room_id
where out_time >= '23:00:00'
group by u.room_id, room_name	
order by user_count desc

发表于 2025-05-18 18:08:21 回复(0)
select b.room_id,b.room_name,count( distinct a.user_id) as user_count from user_view_tb as a
join room_info_tb as b on a.room_id=b.room_id
where hour(a.in_time)>=23&nbs***bsp;hour(a.out_time)>=23
group by b.room_id,b.room_name
order by user_count DESC

发表于 2025-05-14 18:05:36 回复(0)
SELECT r.room_id,
       r.room_name,
       count(t1.user_id)AS user_count
FROM(
SELECT DISTINCT user_id,
       room_id
FROM user_view_tb u
WHERE TIME(in_time) >= '23:00:00' AND TIME(in_time) <= '24:00:00'
&nbs***bsp;TIME(in_time) < '23:00:00' AND TIME(out_time) >= '23:00:00') t1
RIGHT JOIN room_info_tb r
      ON r.room_id = t1.room_id
GROUP BY r.room_id,r.room_name

发表于 2025-05-07 13:47:53 回复(0)
select rit.room_id, rit.room_name,count(distinct uvt.user_id) as user_count
from user_view_tb as uvt
join room_info_tb rit
on(uvt.room_id = rit.room_id)
where uvt.in_time between '23:00:00' and '24:00:00'
or uvt.out_time between '23:00:00' and '24:00:00'
group by rit.room_id, rit.room_name
order by user_count desc
发表于 2025-05-03 12:28:58 回复(0)
select u.room_id,r.room_name,count(distinct u.user_id) as user_count from user_view_tb as u
join room_info_tb as r on u.room_id=r.room_id
where u.in_time <='23:59:59' and u.out_time>='23:00:00'
group by u.room_id,r.room_name
order by user_count desc
distinct 去重
desc 直接用as后的
where 11-12期间,进入时间《=23:59:59,出去时间》=23:00:00,以及,时间要“”
发表于 2025-04-28 11:13:22 回复(0)
select
a.room_id room_id
,room_name
,count(*) user_count
from(select
distinct user_id,
room_id
from user_view_tb
where (in_time) between '23:00:00'  and '24:00:00' or out_time between '23:00:00'  and '24:00:00') a  left join room_info_tb b on a.room_id = b.room_id
group by 1,2
order by 3 desc,1   
我这个有没有优化到位呢?
发表于 2025-04-18 13:48:44 回复(0)
select room_id, room_name, count(1) as user_count
from(
    select distinct a.user_id,a.room_id,    b.room_name
    from user_view_tb a
left join room_info_tb b
on a.room_id=b.room_id  
where left(in_time,2)between 23 and 24 or left(out_time,2)between 23 and 24
)t

group by room_id,room_name
order by  user_count desc,room_id
发表于 2025-04-11 15:53:33 回复(0)
select ub.room_id,any_value(rb.room_name),count(distinct ub.user_id) user_count
from user_view_tb ub
left join room_info_tb rb on ub.room_id = rb.room_id
where(time(ub.out_time) BETWEEN '23:00:00' AND '23:59:59') OR (time(ub.in_time) >= '23:00:00')
group by ub.room_id
order by user_count desc
发表于 2025-04-07 20:58:32 回复(0)
SELECT 
    u.room_id,
    r.room_name,
    COUNT(DISTINCT u.user_id) AS user_count
FROM 
    user_view_tb u
JOIN 
    room_info_tb r ON u.room_id = r.room_id
WHERE 
     u.in_time <= '23:59:59' AND u.out_time >= '23:00:00'
GROUP BY 
    u.room_id, r.room_name
ORDER BY 
    user_count DESC;
没做出来,我给理解成了同一时刻最大在看人数。。
该题的解题思路:表连接-where时间筛选条件-count(distinct user_id)计算在线人数
发表于 2025-04-07 10:27:18 回复(1)
with t2 as
(
select room_id,count(distinct(user_id)) as user_count
from (select * from user_view_tb
    where out_time between time('23:00:00') and time('24:00:00')
    ) as t1
group by room_id)
select t2.room_id,r.room_name,t2.user_count
from t2
left join room_info_tb as r
on t2.room_id = r.room_id
order by user_count desc
发表于 2025-03-27 09:58:20 回复(0)
select
a.room_id,
b.room_name,
count( distinct a.user_id) as user_count
from user_view_tb a
join room_info_tb b on a.room_id = b.room_id
where time(in_time) between '23:00:00' and '23:59:59' or
time(out_time) between '23:00:00' and '23:59:59'
group by a.room_id,b.room_name
order by count(distinct user_id) desc;
发表于 2025-03-25 14:40:43 回复(0)