题解 | #牛客直播开始时各直播间在线人数#
牛客直播开始时各直播间在线人数
https://www.nowcoder.com/practice/bdd30e83d47043c99def6d9671bb6dbf
感谢大佬总结的思路:求某一时刻同时在线人数,只需判断该时刻 between '进入时间' and '退出时间';不过这道题涉及到字符串转时间然后进行时间比较的。这里可以使用str_to_date将str按格式转为datetime然后进行比较。
select ct.course_id, ct.course_name, count(distinct at.user_id) as online_num from course_tb ct join attend_tb at on ct.course_id=at.course_id where str_to_date(substring_index(ct.course_datetime, '-', 3), '%Y-%m-%d %H:%i') between at.in_datetime and at.out_datetime group by ct.course_id, ct.course_name order by ct.course_id asc