如何从mysql获取每小时数据?

时间:2017-02-08 02:48:34

标签: php mysql

我有一个mysql数据库,其中包含我的客户详细信息,我想显示每小时访问次数,数组中的页面浏览量。我在下面给出了mysql查询。

              <?php 
$siteid=$_GET["site"];

          $DB = mysqli_connect('localhost','username','password','databasename');
          $Q = "SELECT time, COUNT(*), pw FROM clientstats WHERE siteid='$siteid' AND time > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 26970 SECOND)) GROUP BY HOUR( FROM_UNIXTIME( `time`) ) ORDER BY time ASC";
          $R = mysqli_query($DB,$Q);
          //start loop
          //while or foreach
          while($row = mysqli_fetch_assoc($R)){

date_default_timezone_set('Asia/Kolkata');
$timestamp = $row['time'];
$timee = date('H', $timestamp);
$times = $row['COUNT(*)']+$row['wh'];


            echo ",['$timee', {$row['COUNT(*)']}, $times]\r\n";

          }
          ?>

其中26970是从午夜到现在的秒数。这段代码工作正常,并输出像这样的数组

,['02', 2, 3] ,['02', 1, 2] ,['04', 1, 2] ,['05', 1, 2] ,['06', 2, 3]

这个数组结构是[&#39; HOUR&#39;,visit,pw],但问题是这不会像1,2,3,4,5,6那样连续显示每小时的值...等我想显示每个小时的价值。如果该小时的值为零,那么我想将该小时值输出为[&#39;小时&#39;,0,0],此外,在第二小时的上述输出中加倍。如何纠正它。

请帮助我,提前致谢。

1 个答案:

答案 0 :(得分:0)

要使用SELECT语句从MySQL返回结果,需要有一个行源,它返回&#34;缺少&#34;从表中我们得到了计数。

(从查询中获取此结果只是一个选项;我们可以采用不同的方法,并在客户端中执行此操作。例如,使用我们想要的小时填充数组,然后运行原始查询,并分配计数到了适当的匹配数组元素。这个答案演示了如何让MySQL返回指定的结果。)

作为我们如何做到这一点的一个例子,这里是一个返回24行整数值0到23的查询。

         SELECT 00 AS hr     UNION ALL SELECT 01 UNION ALL SELECT 02 UNION ALL SELECT 03
         UNION ALL SELECT 04 UNION ALL SELECT 05 UNION ALL SELECT 06 UNION ALL SELECT 07
         UNION ALL SELECT 08 UNION ALL SELECT 09 UNION ALL SELECT 10 UNION ALL SELECT 11
         UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
         UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19
         UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23

我们可以在parens中包装该查询,并在另一个查询中引用该表来代替表。

SELECT i.hr
  FROM ( -- 0 thru 23
         SELECT 00 AS hr     UNION ALL SELECT 01 UNION ALL SELECT 02 UNION ALL SELECT 03
         UNION ALL SELECT 04 UNION ALL SELECT 05 UNION ALL SELECT 06 UNION ALL SELECT 07
         UNION ALL SELECT 08 UNION ALL SELECT 09 UNION ALL SELECT 10 UNION ALL SELECT 11
         UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
         UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19
         UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23
       ) i
  ORDER BY i.hr

还有其他方法可以返回整数集。此示例只是查询中包含的内联视图(派生表)的示例。这并不依赖于数据库中的任何其他表。

如果我们在数据库中有一个包含24行且整数值为0到23的表,我们可以引用它来代替内联视图。

我们还可以向外部查询添加WHERE子句,以排除hr值晚于当前小时的行。

如果我们想要返回两位数小时(前导零),我们可以使用SELECT列表中的表达式。有几种表达方式可以解决问题。

SELECT RIGHT(CONCAT('0',i.hr),2) AS hh
  FROM ( -- 0 thru 23
         SELECT 00 AS hr     UNION ALL SELECT 01 UNION ALL SELECT 02 UNION ALL SELECT 03
         UNION ALL SELECT 04 UNION ALL SELECT 05 UNION ALL SELECT 06 UNION ALL SELECT 07
         UNION ALL SELECT 08 UNION ALL SELECT 09 UNION ALL SELECT 10 UNION ALL SELECT 11
         UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
         UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19
         UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23
       ) i
  WHERE i.hr <= HOUR(NOW())
  ORDER BY i.hr

我们&#34;匹配&#34;从中返回的行,包含从原始查询返回的计数。我们可以使用外连接操作,因此当计数查询中没有匹配的行时,我们会返回一小时的行。

让#34;失踪&#34; count返回为零,我们执行条件测试,并用0替换NULL值。

SELECT RIGHT(CONCAT('0',i.hr,2)) AS hh
     , IFNULL(t.cnt,0) AS visits
     , IFNULL(t.pw,0)  AS pw
  FROM ( -- 0 thru 23
         SELECT 00 AS hr UNION ALL SELECT 01 UNION ALL SELECT 02 UNION ALL SELECT 03
         UNION ALL SELECT 04 UNION ALL SELECT 05 UNION ALL SELECT 06 UNION ALL SELECT 07
         UNION ALL SELECT 08 UNION ALL SELECT 09 UNION ALL SELECT 10 UNION ALL SELECT 11
         UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
         UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19
         UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23
        ) i
  LEFT
  JOIN ( -- counts by hour
         SELECT HOUR(FROM_UNIXTIME(s.time)) AS hr
              , COUNT(*) AS cnt
              , MAX(s.pw) AS pw
           FROM clientstats s
          WHERE s.siteid = '$siteid'
            AND s.time >= UNIX_TIMESTAMP(DATE(NOW()))
            AND s.time <  UNIX_TIMESTAMP(NOW())
          GROUP BY HOUR(FROM_UNIXTIME(s.time))
       ) t
    ON t.hr = i.hr
 WHERE i.hr <= HOUR(NOW())
 ORDER BY i.hr ASC

有几个不同的查询将返回指定的结果。但是返回零计数的技巧是使用行源返回我们想要返回的小时值,即使这些小时值不存在于表格中我们也从