douyi6922 2015-06-04 10:37
浏览 37

MySQL - 根据IP地址和访问时间戳统计所有文章访问者

I've got a table filled with each user's access. One line is one access. In each row there are field like ID, TIMESTAMP, IP_ADDRESS, ARTICLE_ID,...

Now, the problem is an application used for logging users can't be modified by me, but adds 3 rows instead of one - logging not only the desired article but also one on the left and one on the right (based on ARTICLE_ID).

I want to count all article visits eliminating the ballast around. How can I do that? Overall I need to count in middle row's ARTICLE_ID access but leave the surroundings out.

My original idea is to have some PHP function like this (but I am open to ideas):

$logged_article_ids = $wpdb->get_results("SELECT article_id FROM " . $table_name . " GROUP BY article_id", ARRAY_A);

foreach($logged_article_ids as $article) {
// Count in each visit if IP and timestamp is unique or if it is in the middle of 3 lines with equal IP and timestamp.
}

Thank you very much for your help.

Update based on comments: Is ARTICLE_ID unique in the entries? No, it is not. As suggested here is the sample structure:

+---+-----------------+-------------+------------+----
|ID |      TIME       | IP_ADDRESS  | ARTICLE_ID | etc..
+---+-----------------+-------------+------------+----
| 1 | 22-3-2015 11:00 | 134.3.4.104 |     43     |
| 2 | 22-3-2015 11:00 | 134.3.4.104 |     34     |
| 3 | 22-3-2015 11:00 | 134.3.4.104 |     12     |
| 4 | 22-3-2015 11:00 | 32.3.42.203 |     12     |
| 5 | 22-3-2015 11:01 | 32.3.42.203 |     43     |
+---+-----------------+-------------+------------+----

In the above table the desired behaviour would be to count middle line article of same IP_ADDRESS and TIME in and also the other standalone lines. So in this case articles 34, 12 and 43 would have each 1 visit while leaving out article 43 and 12 from IP: 134.3.4.104 at 22-3-2015 11:00. However if time or IP was different, I would like this:

+---+-----------------+-------------+------------+----
|ID |      TIME       | IP_ADDRESS  | ARTICLE_ID | etc..
+---+-----------------+-------------+------------+----
| 1 | 22-3-2015 11:00 | 134.3.4.104 |     43     |
| 2 | 22-3-2015 11:24 | 134.3.4.104 |     34     |
| 3 | 22-3-2015 11:24 | 32.3.42.203 |     12     |
| 4 | 22-3-2015 11:24 | 32.3.42.203 |     12     |
| 5 | 22-3-2015 19:30 | 32.3.42.203 |     43     |
+---+-----------------+-------------+------------+----

Article 43: 2 visitors, 34: 1 visitor, 12: 2 visitors

I thought of adding an additional variable to my above foreach cycle which would remember last cycle's TIMESTAMP and IP_ADDRESS. That is probably a way to go but I haven't been able to test it yet. Also I would prefer a database way but that is too far from my abilities.

Once again thank you very much for your help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

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