douzhi4311 2018-04-28 06:51
浏览 72
已采纳

如何计算mysql和php中多个日期之间的天数?

I have table in mysql which contain start_date, end_date and id like this

start_date   end_date     id
---------------------------------------------
2018-01-01   2018-01-01   5
2018-01-03   2018-01-03   5
2018-01-03   2018-01-08   5
2018-01-06   2018-01-07   7
2018-01-07   2018-01-07   7
2018-01-09   2018-01-11   7
2018-01-02   2018-01-02   8
2018-01-02   2018-01-04   8
2018-01-08   2018-01-08   9

I want output in mysql for total number of days for id like.

total_days  id
-------------------------
7          5
5          7
3          8
1          9

There is day gap between dates of particular ids. Please check start and end dates of each id. I think it need to put all dates in one sequence and then calculate days from first to next. I don't know how to do it. 2018-01-08 to 2018-01-08 should be one day. 2018-01-08 to 2018-01-09 should be 2 days but not 1. Please help. Program is developing in php..

  • 写回答

4条回答 默认 最新

  • dqh1992 2018-05-06 15:24
    关注

    I found the solution that it is not possible in hardcore mysql. I need to do using programming language. As I developed my software using PHP, So I calculated days using PHP code. I align-sort all dates one by other and then calculate days between each date. Thanks for all who gave answers to my question.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化