Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL-按时间间隔选择时间范围中的值_Php_Mysql_Sql - Fatal编程技术网

Php MySQL-按时间间隔选择时间范围中的值

Php MySQL-按时间间隔选择时间范围中的值,php,mysql,sql,Php,Mysql,Sql,我试图从MySQL表中选择按给定间隔分组的时间范围内的值。我的实际代码如下所示: SET @dateformat = '%Y-%m-%d %H:%i:%s'; SET @start = '2014-05-07 17:44:15'; SET @stop = '2014-10-27 15:46:58'; SET @interval = 3600; SELECT t1.id, t1.timestamp, REPLACE(AVG(t1.A_1), '.', ',') AS

我试图从MySQL表中选择按给定间隔分组的时间范围内的值。我的实际代码如下所示:

SET @dateformat = '%Y-%m-%d %H:%i:%s';
SET @start = '2014-05-07 17:44:15';
SET @stop = '2014-10-27 15:46:58';
SET @interval = 3600;

SELECT  
    t1.id,
    t1.timestamp,
    REPLACE(AVG(t1.A_1), '.', ',') AS avg_val1 
        FROM eco_547702f977d27 AS t1 WHERE timestamp > @start AND timestamp <= @stop GROUP BY UNIX_TIMESTAMP(timestamp) DIV @interval;
SET@dateformat='%Y-%m-%d%H:%i:%s';
开始时设置='2014-05-07 17:44:15';
设置停止时间='2014-10-27 15:46:58';
设置@interval=3600;
挑选
t1.id,
t1.时间戳,
将(平均值(t1.A_1),“,”,“)替换为平均值1

从eco_547702f977d27作为t1,其中timestamp>@start和timestamp使用一个非常简单的技巧:


将44:15添加到时间值,然后执行相同的
分组操作
,您将获得所需分组中的数据

我认为最简单的方法就是从值中减去44*60+15秒,并将其用于间隔算术。鉴于您的表达方式:

SELECT t1.id, t1.timestamp, REPLACE(AVG(t1.A_1), '.', ',') AS avg_val1 
FROM eco_547702f977d27 AS t1
WHERE timestamp > @start AND timestamp <= @stop
GROUP BY (UNIX_TIMESTAMP(timestamp) - (44*60 + 15)) DIV @interval;

还有一个问题是,@间隔可以在5秒到12个月之间变化!结果开始不同:2014-05-07 17:44:15.000。。。2014-05-07 20:44:14.000 ... 2014-05-08 05:44:13.000…@Mitterer。要解决第二个问题,请尝试使用第二个查询。
SELECT t1.id
       FROM_UNIX_TIMESTAMP((UNIX_TIMESTAMP(timestamp) - (44*60 + 15)) DIV @interval) as timestamp,
       REPLACE(AVG(t1.A_1), '.', ',') AS avg_val1 
FROM eco_547702f977d27 AS t1
WHERE timestamp > @start AND timestamp <= @stop
GROUP BY (UNIX_TIMESTAMP(timestamp) - (44*60 + 15)) DIV @interval;