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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?