我希望在图表中获取访问我网页10天的用户的价值。我需要COUNT()过去十天的所有值。
最佳布局是
Day|COUNT(ip)
1 - 10
2 - 12
3 - 52
......
我希望你理解我的意思。 MySQL可以直接执行此操作还是需要我在PHP中以10个单独的查询执行此操作?
此致 Moritz的
使用Tablestructure更新:
Id (Auto Increment)|Time (Unix Timestamp)|Ip|Referer
答案 0 :(得分:2)
这将为您提供具有实际日期值的结果:
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')
答案 1 :(得分:2)
这应该可以快速运行
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`);
答案 2 :(得分:1)
试试这个:
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)