如何选择每个日期间隔的唯一访客?

发布于 2024-11-18 04:47:58 字数 356 浏览 4 评论 0原文

我找到了一些与我要查找的内容相关的查询字符串,但没有任何效果。基本上,我有一个图表需要显示每个时间间隔(小时、天、月或年)的唯一访问者。因此,类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔落惊风雨 2024-11-25 04:47:58

您将需要汇总您的数据。尝试此查询:

SELECT DATE(data_timestamp), COUNT(visitor_id)
FROM analytics_table
WHERE DATE(data_timestamp) BETWEEN '2011-05-01' AND '2011-05-31'
GROUP BY 1

以下是填充每月数据的方法(警告:未经测试!):

<?php
$sql = "SELECT DATE(data_timestamp), COUNT(visitor_id)
FROM analytics_table
WHERE DATE(data_timestamp) BETWEEN '2011-05-01' AND '2011-05-31'
GROUP BY 1";

$rs = mysql_query($rs);
$date1 = $datex = '2011-05-01';
$date2 = '2011-05-31';

$arrayData = $tmpArray = array();

while( $r = mysql_fetch_array($rs) )
{
   $tmpArray[$r['date']] = $r['count'];
}

while( $datex <= $date2)
{
   if( isset($tmpArray[$datex]) )
   {
      $arrayData[$datex] = $tmpArray[$datex];
   }
   else
   {
      $arrayData[$datex] = 0;
   }
   list( $y, $m, $d) = explode('-', $datex);
   $datex = date('Y-m-d', mktime(0, 0, 0, $m, $d, $y));
}


?>

此查询使用:

  • DATE() 从数据中获取日期,并使用
  • COUNT() 计算该特定数据的总数据日期。
  • 和 GROUP BY 根据您选择的字段对数据进行分组。

You will need to aggregate your data. Try this query:

SELECT DATE(data_timestamp), COUNT(visitor_id)
FROM analytics_table
WHERE DATE(data_timestamp) BETWEEN '2011-05-01' AND '2011-05-31'
GROUP BY 1

Here's how you populate your monthly data (WARNING: UNTESTED!):

<?php
$sql = "SELECT DATE(data_timestamp), COUNT(visitor_id)
FROM analytics_table
WHERE DATE(data_timestamp) BETWEEN '2011-05-01' AND '2011-05-31'
GROUP BY 1";

$rs = mysql_query($rs);
$date1 = $datex = '2011-05-01';
$date2 = '2011-05-31';

$arrayData = $tmpArray = array();

while( $r = mysql_fetch_array($rs) )
{
   $tmpArray[$r['date']] = $r['count'];
}

while( $datex <= $date2)
{
   if( isset($tmpArray[$datex]) )
   {
      $arrayData[$datex] = $tmpArray[$datex];
   }
   else
   {
      $arrayData[$datex] = 0;
   }
   list( $y, $m, $d) = explode('-', $datex);
   $datex = date('Y-m-d', mktime(0, 0, 0, $m, $d, $y));
}


?>

this query use:

  • DATE() to get the date from your data and
  • COUNT() to count total data from that particular date.
  • and GROUP BY to group your data based on what field you select.
情深已缘浅 2024-11-25 04:47:58

基本上,您可以将 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

童话里做英雄 2024-11-25 04:47:58

如果某一天没有访客,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文