douju1365 2009-12-18 05:31
浏览 68
已采纳

如何按月在两个指定日期之间进行迭代

I have a website that was launched a few months ago. I would like to write some code that will look through every month, since launch, and grab metrics for that month.

My question is: What is the best way to specify a start date, and then iterate by month, all the way up until the current month (or to another specified date).

Does anyone have any suggestions? I'm using PHP and a mySQL database.

  • 写回答

5条回答 默认 最新

  • douzong2206 2009-12-18 06:02
    关注

    I don't know the schema of your stored metrics, so here's a generic example:

    Select total pageviews per month from Aug 1 thru Nov 30 2009

    The code for MySQL:

    SELECT DATE_FORMAT(s.date, '%Y-%m') AS  year_month , SUM( s.pageviews ) AS  s.pageviews_total 
    FROM statistics s
    WHERE s.date BETWEEN '2009-08-01' AND '2009-11-30'
    GROUP BY DATE_FORMAT(s.date, '%Y-%m') 
    ORDER BY DATE_FORMAT(s.date, '%Y-%m')
    

    Having that aggregated table output may be enough for you. If not, you can loop through it with PHP and perform other manipulations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?