使用PHP定期检索mysql统计数据并包括空数据
因此,我正在尝试为我的应用程序构建一些不错的统计数据显示。我可以做到这一点,因为我将命中统计数据保存在表格中。它只是跟踪点击次数以及其他一些有用的数据及其发生的时间。我可以查询数据库以显示特定一天或过去 x 天的每一天发生了多少次点击,如下面的代码所示。但是,下面的代码仅返回有数据的日期。我想显示过去 30 天的点击量,无论某一天是否有点击量。想法?
SELECT DATE(time) AS theday, COUNT( * ) AS thecount
FROM stats
WHERE time <= curdate( )
AND time >= DATE_SUB( curdate(), INTERVAL 30 DAY )
GROUP BY theday ORDER BY time DESC
正如您所看到的,
theday thecount
2011-11-22 5
2011-11-21 9
2011-11-18 10
2011-11-16 1
2011-11-11 2
2011-11-10 15
2011-11-09 2
2011-10-26 1
2011-10-24 6
它跳过了没有结果的日期。我明白为什么会这样,因为数据库中没有包含这些日期的行。我想知道如何生成一个几乎与上面类似但具有所述间隔的所有日期的查询。 IE:过去 30 天。
So I am trying to build out some nice stats displays for my app. I can do this because I keep hit stats in a table. It simply tracks the hits plus some other nice data and the time it occurs. I can query the db to show me how many hits occurred on a particular day or on each day for the past x days as in the code below. However the code below only returns dates for which there is data. I would like to show the last 30 days of hits regardless of whether a day has a hit or not. Thoughts?
SELECT DATE(time) AS theday, COUNT( * ) AS thecount
FROM stats
WHERE time <= curdate( )
AND time >= DATE_SUB( curdate(), INTERVAL 30 DAY )
GROUP BY theday ORDER BY time DESC
Produces
theday thecount
2011-11-22 5
2011-11-21 9
2011-11-18 10
2011-11-16 1
2011-11-11 2
2011-11-10 15
2011-11-09 2
2011-10-26 1
2011-10-24 6
As you can see it skips dates with no results. I understand why this is, as there are no rows with those dates in the db. I am wondering how I would produce a query that works almost like the above but has all the dates for said interval. IE: the last 30 days.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有 3 个选择:
应用程序逻辑实现示例:
You have 3 options:
Example for app logic implementation: