duanping2809 2011-06-30 02:04
浏览 36
已采纳

如何按日期间隔选择唯一身份访问者?

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)?

  • 写回答

3条回答 默认 最新

  • dpmrakfbx820320638 2011-06-30 02:15
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码