如何选择每个日期间隔的唯一访客?
我找到了一些与我要查找的内容相关的查询字符串,但没有任何效果。基本上,我有一个图表需要显示每个时间间隔(小时、天、月或年)的唯一访问者。因此,类似于 Google Analytics 的图表或此分析图表。该表有一个访问者 ID 列和一个时间戳列(样式为 0000-00-00 00:00:00)。我需要能够选择每个时间间隔的唯一访客或总访客(例如 5 日 32 名访客,6 日 30 名访客等)。
我想我的问题是,是否有办法仅通过查询就可以有效地完成此操作?或者我需要使用 PHP 来获取所有数据(问题是我必须对图表上的每个点进行查询,效率不高)?
I've found a few query strings related to what I'm looking for, but nothing that does this. Basically, I have a graph that needs to display unique visitors per time interval (hour, day, month, or year). So, a graph similar to Google Analytics or this Analytics Graph. The table has a visitor ID column and a timestamp column (in the style 0000-00-00 00:00:00). I need to be able to select the unique or total visitors per interval (eg. 32 visitors on the 5th, 30 on the 6th, etc).
I guess my question is, is there anyway to do this efficiently with just a query? Or will I need to use PHP to get all the data (problem with that is that I would have to do a query for each point on the graph, not efficient)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将需要汇总您的数据。尝试此查询:
以下是填充每月数据的方法(警告:未经测试!):
此查询使用:
You will need to aggregate your data. Try this query:
Here's how you populate your monthly data (WARNING: UNTESTED!):
this query use:
基本上,您可以将 DATETIME/TIMESTAMP 分别转换为日期和时间,然后使用 GROUP BY 来获取您需要的结果。
MySQL/SQL:仅在日期时间列上按日期分组
Basically, you can cast the DATETIME/TIMESTAMP into dates, and times separately, and then use a GROUP BY to get the results you need.
MySQL/SQL: Group by date only on a Datetime column
如果某一天没有访客,GROUP BY 不会返回 0 值。这将导致时间连续性上的漏洞。有两种方法:
1)像其他人所说的那样分组,然后找到漏洞
2)创建包含“日期”,“计数”等字段的聚合表,并通过cron每天插入数据。
GROUP BY doesn't return 0 value if there is no visitor some day. That will yield to the holes in time continuity. There are two ways:
1) Group like others said and then find holes
2) Create aggregation table with fields "date", "count" etc. and insert there data every day by cron.