MySQL显示范围内的所有日期

MySQL display all date in between range(MySQL显示范围内的所有日期)
本文介绍了MySQL显示范围内的所有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示 MySQL 中 fromto 日期之间的所有日期.

I want to display all dates between a from and to dates from MySQL.

例如 schedule 表中具有 fromto 字段的数据是:

For example the data from schedule table that has from and to fields are:

from 日期为 2013-3-13,且

日期是2013-3-20

我想要的结果是:

 2013-3-13
 2013-3-14
 2013-3-15
 2013-3-16
 2013-3-17
 2013-3-18
 2013-3-19
 2013-3-20

如何仅使用 MySQL 查询来实现这一点(不必使用存储过程,因为我不熟悉它)?

How can I achieve this using MySQL query only (without having to use stored procedure 'cause I'm not familiar with it)?

这里的答案很有帮助,虽然我还没有完全明白想要什么.在this sample中,它只运行成功,没有输出任何东西.而且我不知道问题出在哪里.

The answer here is very helpful, though I still don't fully get what is desired. In this sample, it only runs successfully but doesn't output anything. And I don't know what seems to be the problem.

请帮忙.谢谢!

推荐答案

您可以使用以下方法来生成日期列表:

You can use the following to generate your list of dates:

select a.Date,  s.*
from 
(
  select curdate() + INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
  from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
  cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
  cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
inner join schedule s
  on a.Date >= s.fromDate 
  and a.Date <= s.toDate

请参阅 SQL Fiddle with Demo

这篇关于MySQL显示范围内的所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)