我有一点问题要做统计。
人们可以在我的网站上观看视频。我将一些数据存储在mysql表videos_has_views
中IdUser = the watcher ID
ddv = date of view = the start time of the view
duration = the duration of the view
我想得到的是一个数组,其中有多少观众在时间范围内活跃。
例如:
John start viewing at 2017-01-01 00:00:00 and watch video for 1800 seconds (30mn)
Hodor start viewing at 2017-01-01 00:00:00 and watch video for 1200 seconds (20mn)
Rob start viewing at 2017-01-01 00:10:00 and watch video for 1800 seconds (30mn)
我尝试使用
这样的数组From 0 to 10min : 2 viewers (john + hodor)
From 10 to 20min : 3 viewers
From 20 to 30 : 2 viewers (john + Rob)
From 30 to 40 : 1 viewer (rob)
但是我不知道如何使用mysql攻击这个问题...或者在php中使用循环? 如果有人对我有所了解,我会非常感激
答案 0 :(得分:0)
也许是这样的?
SELECT
CASE
WHEN duration BETWEEN 0 AND 600 THEN '0to10'
WHEN duration BETWEEN 601 AND 1200 THEN '10to20'
WHEN duration BETWEEN 1201 AND 1800 THEN '20to30'
ELSE 'moreThan30'
END as duration_range,
count(IdUser) as cnt
FROM videos_has_views
GROUP BY duration_range
ORDER BY duration_range ASC;
你可以根据需要创建多个范围......它可以工作(至少在MySQL 5.7中) 如果你想在另一列中包含名称,那么(因为我们正在使用GROUP BY)你可以使用来自names / userIDs或其他的GROUP CONCAT:)