0

我可以选择单个时间范围内的唯一 IP 地址总数

SELECT COUNT(DISTINCT ip_address) as ip_addr, exec_datetime
FROM requests
WHERE exec_datetime >= NOW() - INTERVAL 1 DAY;

ip_addr exec_datetime
45      12/10/2012 5:21

如何在单个查询中返回以下子句的结果集...

WHERE exec_datetime >= NOW() - INTERVAL 1 DAY;
WHERE exec_datetime >= NOW() - INTERVAL 2 DAY;
...
WHERE exec_datetime >= NOW() - INTERVAL 14 DAY;

...所以结果集看起来像这样?

ip_addr exec_datetime
45      11/26/2012 5:21
85      11/27/2012 5:21
130     11/28/2012 5:21
170     11/29/2012 5:21
...     ...
4

1 回答 1

1

我会使用这样的东西:

SELECT
  COUNT(DISTINCT requests.ip_address) ip_addr,
  days.day
FROM
  (SELECT now()-INTERVAL 1 DAY as day UNION
   SELECT now()-INTERVAL 2 DAY UNION
   ...
   SELECT now()-INTERVAL 14 DAY) days
  INNER JOIN requests
  on requests.exec_datetime >= days.day
GROUP BY
  days.day
于 2012-12-11T06:59:20.567 回答