MySQL数据以10分钟为间隔计数

时间:2016-02-01 12:23:21

标签: mysql database datetime time group-by

我有一张表格,记录学生离开的日期时间及其详细信息。

我想要做的是计算在两次设定时间内留出的学生数量,每隔10分钟列出所有时间。

此脚本将计算离开的学生人数,但仅当学生在10分钟的时间间隔内离开时,它才会跳过任何没有学生离开的10分钟。

如果没有学生在该时间间隔内离开,我需要它返回0给出的日期时间之间的所有时间间隔,以便不会错过任何时间。

SELECT
    FROM_UNIXTIME(
        CEILING(
            UNIX_TIMESTAMP(`time_left`) / 600
        ) * 600
    ) AS timeslice,
    COUNT(*) AS mycount
FROM
    tbl_student_log
WHERE
    `time_left` >= '2016-02-01 12:00:00'
AND `time_left` < '2016-02-01 17:00:00'
GROUP BY
    timeslice

SQL Fiddle

测试数据库:

CREATE TABLE `tbl_student_log` (
`id`  int(5) UNSIGNED NOT NULL AUTO_INCREMENT ,
`student_id`  int(5) NOT NULL ,
`time_left`  datetime NULL ,
PRIMARY KEY (`id`)
);

INSERT INTO `tbl_student_log` VALUES (1, 2, '2016-02-01 12:06:24');
INSERT INTO `tbl_student_log` VALUES (2, 4, '2016-02-01 12:13:42');
INSERT INTO `tbl_student_log` VALUES (3, 5, '2016-02-01 12:14:01');
INSERT INTO `tbl_student_log` VALUES (4, 8, '2016-02-01 14:07:25');

SQL

SELECT
    FROM_UNIXTIME(
        CEILING(
            UNIX_TIMESTAMP(`time_left`) / 600
        ) * 600
    ) AS timeslice,
    COUNT(*) AS mycount
FROM
    tbl_student_log
WHERE
    `time_left` >= '2016-02-01 12:00:00'
AND `time_left` < '2016-02-01 17:00:00'
GROUP BY
    timeslice

以上数据的输出是:

12:10 = 1  
12:20 = 2  
14:10 = 1  

我需要的实际输出是:

12:00 = 0  
12:10 = 1  
12:20 = 2  
12:30 = 0  
12:40 = 0  
...  
14:10 = 1  
...  
17:00 = 0  

日期之间的所有时间都返回一个值

2 个答案:

答案 0 :(得分:1)

我已经设法使用PHP和MySQL解决了我的查询,因为我还没能找到纯粹的MySQL解决方案。

首先,我使用时间间隔作为关键字设置一个时间数组,并将默认值设置为0

$times       = array();
$start_time  = "2016-02-01 12:00:00";
$end_time    = "2016-02-01 17:00:00";
$interval    = "+10 minutes";

$current = $start_time;
while( strtotime($current) <= strtotime($end_time) )
{
    $times[date('H:i', strtotime($current))] = 0;
    $current = date("Y-m-d H:i", strtotime($interval, strtotime($current)));
}

然后使用我的查询结果,我将数组更新为非0

字段的值
foreach($result as $row)
{
    $times[date('H:i', strtotime($row['timeslice']))] = $row['mycount'];
}

不确定是否有人能想出一个纯粹的MySQL解决方案。

答案 1 :(得分:1)

  

哦......男人,你的问题困难,花了 5小时之后   这个查询。我使用查询 找到解决方案

步骤:

1.) you have to create procedure. Click here

2.) Download it and import "generate_series.sql" in your table.
3.) You have to run two queries :
    3.1) 1st you have to call procedure.
    3.2) 2nd you have to write another query.

在表格中导入 generate_series.sql 后,您必须编写如下查询:

CALL generate_series_date_minute('2016-02-01 11:50', '2016-02-01 17:00:00', 10);

SELECT (s.series + interval 10 MINUTE) AS timeslice, count(t.time_left) AS mycount
FROM series_tmp AS s LEFT JOIN tbl_student_log AS t
ON true
AND t.time_left > s.series
AND t.time_left <= (s.series + interval 10 MINUTE)
GROUP BY s.series limit 31

注意:如果您想将间隔从10更改为15或20,或者您必须更改查询和程序,则还需要编写来自日期在程序中。您甚至可以更改程序(请参阅documentation

示例: generate_series_date_minute(from,to,interval);

输出:

timeslice                   mycount
2016-02-01 12:00:00           0
2016-02-01 12:10:00           1
2016-02-01 12:20:00           2
2016-02-01 12:30:00           0
2016-02-01 12:40:00           0
2016-02-01 12:50:00           0
2016-02-01 13:00:00           0
2016-02-01 13:10:00           0
2016-02-01 13:20:00           0
2016-02-01 13:30:00           0
2016-02-01 13:40:00           0
2016-02-01 13:50:00           0
2016-02-01 14:00:00           0
2016-02-01 14:10:00           1
2016-02-01 14:20:00           0
2016-02-01 14:30:00           0 
2016-02-01 14:40:00           0
2016-02-01 14:50:00           0
2016-02-01 15:00:00           0
2016-02-01 15:10:00           0
2016-02-01 15:20:00           0
2016-02-01 15:30:00           0
2016-02-01 15:40:00           0
2016-02-01 15:50:00           0
2016-02-01 16:00:00           0
2016-02-01 16:10:00           0
2016-02-01 16:20:00           0
2016-02-01 16:30:00           0
2016-02-01 16:40:00           0
2016-02-01 16:50:00           0
2016-02-01 17:00:00           0