我可以选择单个时间范围内唯一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
... ...
最佳答案
我会用这样的东西:
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
关于mysql - 如何在exec_datetime> = NOW()-INTERVAL n DAY的几个不同时间范围内获取唯一值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13814141/