【问题标题】:How to echo whole week statistics from mysql table?如何从 mysql 表中回显整周的统计信息?
【发布时间】:2014-05-27 13:21:01
【问题描述】:

我一直在想,我自己解决不了这个问题。

我有这个 mysql 表,其中包含网站访问者的统计信息

+-----------------------------+
+ date        | visits        +
+-----------------------------+
+  2014-03-02 | 736           +
+  2014-03-03 | 936           +
+  2014-03-06 | 54            +                 
+-----------------------------+

我想回应一周的统计报告,但也显示表格中没有数据的天数。

输出应该是:

2014-03-01: 0
2014-03-02: 736
2014-03-03: 936
2014-03-04: 0
2014-03-05: 0
2014-03-06: 54
2014-03-07: 0

请注意,我知道如何使用此功能:

$first = '2014-03-01';

$last = '2014-03-07';

while (strtotime($first) <= strtotime($last)) {

$related10 = mysql_query("SELECT * FROM stats WHERE date >= '$first' and date <= '$last'");
$rows = mysql_fetch_array($related10);
$date = $rows['date'];
$visits = $rows['visits'];

echo ''.$date.': '.$visits.'';

$first = date("Y-m-d", strtotime("+1 day", strtotime($first)));
}

但是这个函数的问题是它为每个日期运行一个 sql 查询,并且对于较大的日期范围,它在加载页面时持续几分钟。

我想用一个 sql 查询来完成这项工作

应该是这样的

$related10 = mysql_query("SELECT * FROM stats WHERE fecha >= '$first' and fecha <= '$last'");

while($rows = mysql_fetch_array($related10)) {

//function to add the missing date data.
echo $output;

}

有人知道如何解决这个问题吗?

非常感谢您。

【问题讨论】:

标签: php mysql sql loops if-statement


【解决方案1】:

Rafal 打败了我,这里是大意。请注意,我在这里运行了两个循环,但您可能会进一步改进算法。

// first put the date value into the "key" of each array item
$rows2 = array();
foreach ($rows as $row) {
    $rows2[$row['date']] = $row['visits'];
}


$currentTs = strtotime('2014-03-01');
$lastTs = strtotime('2014-03-07');

while ($currentTs < $lastTs) {
    $displayDate = date('Y-m-d', $currentTs);
    echo $displayDate . " = ";
    if (!empty($rows2[$displayDate])) {
        echo $rows2[$displayDate]; // echo the visits
    }
    else {
        echo '0';
    }

    echo "\n";
    $currentTs = strtotime('+1 day', $currentTs);
}

【讨论】:

    【解决方案2】:

    你的表现是对的!

    您可以一次获取所有记录并将它们保存在一个数组中,其中日期是关键。然后,在您的循环中,您可以检查该日期是否存在于您的数组中。

    【讨论】:

      【解决方案3】:

      如果您愿意创建一个小型实用程序表,您可以使用this solution (SQLFiddle)

      CREATE TABLE ints ( i tinyint );
      
      INSERT INTO ints VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
      

      然后在 PHP 中

      $related10 = mysql_query("
      SELECT calendar.date,
             ifnull(stats.visits, 0) AS visits
        FROM (SELECT DATE('$first') + INTERVAL a.i*100 + b.i*10 + c.i DAY AS date
            FROM ints a
            JOIN ints b
            JOIN ints c
           WHERE (a.i*100 + b.i*10 + c.i) <= DATEDIFF('$last', '$first')
              ) calendar
         LEFT JOIN stats ON stats.date = calendar.date
        ORDER BY calendar.date
      ");
      

      如果您需要处理超过 1000 天的差异,只需将连接添加到 ints 表并乘以 10 的幂,直到您可以覆盖所需的天数。感谢 http://www.brianshowalter.com/calendar_tables 的 Brian Showalter 提供 SELECT 日历数据的想法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-09
        • 2014-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        相关资源
        最近更新 更多