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条)

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源