step1:选择观看时间包含在20:00-23:00点的直播的信息;
step2: 设置时间,如果开始时间小于20:00,设置为20:00,如果结束时间大于23:00,将结束时间设置为23:00;
step3: 左连接直播间信息表;
step4:按照直播间类型分组,将所有结束时间减去开始时间的差值除以60(分钟)求和;
step5:按观看总时长降序排序。
SELECT room_type, ROUND(SUM((UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time))/60), 2) as sum_view_minute
FROM
(select *,
IF(in_time<='20:00:00', time('20:00:00'), in_time) as start_time,
IF(out_time>='23:00:00', time('23:00:00'), out_time) as end_time
from user_view_tb
where
(in_time<='20:00:00' and out_time>='20:00:00')
OR(in_time BETWEEN '20:00:00' and '23:00:00')
)a
left join room_info_tb b
on a.room_id=b.room_id
group by room_type
order by sum_view_minute desc;