我想在图表中获取访问我页面 10 天的用户的价值。我需要 COUNT() 过去十天的所有值。
最好的布局是
Day|COUNT(ip)
1 - 10
2 - 12
3 - 52
......
我希望你明白我的意思。MySQL 可以直接执行此操作,还是需要我在 PHP 中以 10 个单独的查询执行此操作?
问候,莫里茨
使用表结构更新:
Id (Auto Increment)|Time (Unix Timestamp)|Ip|Referer
这将为您提供具有实际日期值的结果:
SELECT
COUNT(DISTINCT ip),
FROM_UNIXTIME(Time, '%m/%d/%Y') AS Day
FROM
tbl
WHERE
Time >= UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -10 DAY))
GROUP BY
FROM_UNIXTIME(Time, '%m/%d/%Y')
这应该为您快速运行
SELECT COUNT(ip) ipcount,dt FROM
(
SELECT ip,DATE(FROM_UNIXTIME(`Time`)) as dt FROM mytable
WHERE `Time` > TO_UNIXTIME(NOW() - INTERVAL 10 DAY)
) A GROUP BY dt;
确保你有一个索引Time
ALTER TABLE mytable ADD INDEX TimeIndex (`Time`);
试试这个:
SELECT CAST(DATE(FROM_UNIXTIME(`Time`)) AS CHAR) as dateoftime, COUNT(Ip) as cnt
FROM tablename
WHERE DATE(FROM_UNIXTIME(`Time`)) > DATE_SUB(current_timestamp, INTERVAL 10 DAY)
GROUP BY CAST(DATE(FROM_UNIXTIME(`Time`)) AS CHAR)