简体   繁体   English

MySQL-每天每个IP最多统计1次唯一视图

[英]MySQL - Count unique views per IP max 1 time per day

I'm trying to get unique views per IP, counted max 1 time per day returned using a MySQL query. 我正在尝试获取每个IP的唯一视图,使用MySQL查询每天最多返回1次。

The table is looking like this: 该表如下所示:

product_id          time          ip
INT                 TIMESTAMP     VARCHAR

The query should return the unique views for any time, but it should filter product id's so each product have it's own unique views field counted. 该查询应随时返回唯一视图,但它应过滤产品ID,以便每个产品都具有自己的唯一视图字段。 The IP-address in the table should be counted MAX once per day. 表格中的IP地址每天应最多计算一次。 I'll give you some details what should and should not be counted in the query below: 我将为您提供一些详细信息,在下面的查询中应该和不应该计数:

product_id          DATE(time)          ip            should_be_counted
11                  2012-12-14          1.1.1.1       YES
11                  2012-12-14          1.1.1.2       YES
11                  2012-12-14          1.1.1.1       NO
11                  2012-12-13          1.1.1.1       YES
11                  2012-12-13          1.1.1.1       NO

Should be returned:
product_id          unique_views
11                  3

This is what I want: 这就是我要的:

product_id          unique_views
11                  103
8                   53
2                   3
1                   1

Which means that the query should use ORDER BY unique_views DESC. 这意味着查询应使用ORDER BY unique_views DESC。

Thanks for helping! 感谢您的帮助!

SELECT Product_ID, SUM(unique_view) unique_views
FROM
(
  SELECT Product_ID, COUNT(DISTINCT IP)  unique_view
  FROM   tableName
  GROUP BY Product_ID, DATE(time)
) x
GROUP BY Product_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM