QQ咨询不加好友发不了信息,咨询前先加好友! → QQ:820896380

如何从 MySQL 中的日期范围生成天数?

如何从 MySQL 中的日期范围生成天数? - 我爱模板网

可以借助以下使用 adddate() 函数的查询来完成,我们生成“2016-12-15”和“2016-12-31”之间的天数 -

  1. mysql> select * from
  2. -> (select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date from
  3. -> (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
  4. -> (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
  5. -> (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
  6. -> (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
  7. -> (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
  8. -> Where gen_date between '2016-12-15' and '2016-12-31'
  9. -> ;
  10. +------------+
  11. | gen_date |
  12. +------------+
  13. | 2016-12-15 |
  14. | 2016-12-16 |
  15. | 2016-12-17 |
  16. | 2016-12-18 |
  17. | 2016-12-19 |
  18. | 2016-12-20 |
  19. | 2016-12-21 |
  20. | 2016-12-22 |
  21. | 2016-12-23 |
  22. | 2016-12-24 |
  23. | 2016-12-25 |
  24. | 2016-12-26 |
  25. | 2016-12-27 |
  26. | 2016-12-28 |
  27. | 2016-12-29 |
  28. | 2016-12-30 |
  29. | 2016-12-31 |
  30. +------------+
  31. 17 rows in set (0.30 sec)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

请我们喝杯咖啡,谢谢^_^

给TA打赏
共0人
如本文“对您有用”,欢迎随意打赏,金额不重要,认可最重要!
    豆包可以帮你高效完成AI问答、AI对话、提供软件相关教程以及解决生活中遇到的各种疑难杂症,还能帮助你进行AI写作、AI绘画等等,提高你的工作学习效率。
    !
    你也想出现在这里?立即 联系我们吧!
    信息
    个人中心
    购物车
    优惠劵
    今日签到
    搜索