mySQL,网站统计:每日独特访问

时间:2017-03-23 13:07:32

标签: mysql

我在数据库中有一个表statistics

CREATE TABLE `statistics` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `address` TEXT NOT NULL,
  `ip` TEXT DEFAULT NULL,
  PRIMARY KEY (`id`)
)

要获取每日网页访问次数,我使用以下查询:

SELECT DATE_FORMAT(`timestamp`, "%Y-%m-%d"), COUNT(*)
FROM `statistics`
GROUP BY 1
ORDER BY 1 ASC

如何修改查询以获取IP独有的每日访问次数?

非常感谢!

2 个答案:

答案 0 :(得分:1)

将其更改为计算不同的IP,如下所示

ObjectMapper mapper = ...

String json = "{\"description\": \"new description\"}";

mapper.readerForUpdating(objectToUpdate).readValue(json);

如果您想将null视为单个IP,我们必须进行稍微不同的查询

SELECT DATE_FORMAT(`timestamp`, "%Y-%m-%d"), COUNT(DISTINCT `ip`)
FROM `statistics`
GROUP BY 1
ORDER BY 1 ASC

答案 1 :(得分:1)

您可以在IPSELECT中添加GROUP BY,例如:

SELECT DATE_FORMAT(`timestamp`, "%Y-%m-%d"), IP, COUNT(*)
FROM `statistics`
GROUP BY 1, 2
ORDER BY 1 ASC