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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度