Heim  >  Artikel  >  Backend-Entwicklung  >  关于PHP组织密度不统一数据的系列有关问题

关于PHP组织密度不统一数据的系列有关问题

WBOY
WBOYOriginal
2016-06-13 13:34:06712Durchsuche

关于PHP组织密度不统一数据的系列问题
小弟能力有限,在做一个小项目时碰见了一个问题,希望大家能帮助一下。

数据库cfg_hisdata表用于存放采集到的设备历史参数信息,其中有三个关键字段,TimeH(用于存放6位年月日),TimeL(用于存放6位时分秒),ParamContent(用于存放设备参数)。

存放的规则是每整小时存放一次,此外当参数发生变化时也存放一次(精确到秒)。

所以对我而言,我从库中得到的数据密度并不均匀。而我需要从查询日期0时到24时将数据以分钟为单位(可以忽略秒)组织,然后传递到前台。


我之前的解决思路大体如下

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->// 将得到的日期补全,并且计算出天数,用天数乘以1440*60得到秒数
    $dateStart  = 20120420;  // 查询起始日期
    $dateEnd    = 20120420;  // 查询截至日期
    $day=(strtotime($dateEnd)-strtotime($dateStart))/3600/24 + 1;  // 计算查询天数

    $ltime = date('YmdHis',  mktime(0, 0, 0, substr($dateStart, 4, 2), substr($dateStart, 6, 2), substr($dateStart, 0, 4)));  // 生成查询起始日期的0点0分

    for ($i = 0; $i 


以上是因为数据精度为秒,所以构建一个key为秒的数组

之后
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
while($result_va = mysqli_fetch_assoc($query))
        {
            $temp_data = $result_va['ParamContent'];
            $temp_time = $result_va['TimeH'].$result_va['TimeL'];
            $secArr[$temp_time] = (float)$temp_data;
            
            if ($cache_d)  // 防止复写数组的起始位置
            {
                $secArr = array_fill($cache_t, (int)$temp_time - $cache_t, $cache_d);  // 用前一个有效值填补到当前有效值数组中的空值
            }
            
            // 将当前有效值以及时间作为填充值备用
            $cache_d = $temp_data;
            $cache_t = $temp_time; 
        }



也就是在遍历查询结果的时候,根据其时标放入之前组织好的秒级数组中,在遍历下一个记录时,用上一个记录的值填补他们之间空余的值。


当这个数组组织好后,我准备再以60为一间隔遍历这个秒级的数组,将其放入分钟为单位的新数组中最后输出。


但是感觉自己的想法太过于繁琐,而且循环次数太多,太不效率。
自己接触开发不久,水平属于入门层次。还请各位指点一下,不胜感激

------解决方案--------------------
也就是说在得到的每一分钟的数据是这一分钟最后写入的数据,这样理解对吧?
于是事情就变得简单了:
查询串写作
$sql =SELECT * FROM 表 where TimeH=' $dateStart' and 
timel in (select max(timel) from 表 where TimeH=' $dateStart' group by left(timel,4))
SQL;
稍加修改就可跨日

读取后你只需判断分钟是否连续就可以了
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn