如何在表格中显示多个两个日期之间的所有日期?

帕德曼纳森(Padmanathan j)

我想显示特定记录的两个日期之间的所有日期

这是表格:

ID Start_Date End_Date

1 2013-01-14 2013-01-18
2 2013-02-01 2013-02-04

现在我想获取从日期到日期之间的所有日期。

预期产量

ID Date
1 2013-01-14
1 2013-01-15
1 2013-01-16
1 2013-01-17
1 2013-01-18
2 2013-02-01
2 2013-02-02
2 2013-02-03
2 2013-02-04

指导我为此编写查询,而无需创建任何额外的表。

我已经尝试过以下查询

select * from 
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date from
 (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,
 (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,
 (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,
 (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,
 (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
where selected_date between '2012-02-10' and '2012-02-15'

它的工作记录很好。但我想从表中获取所有日期间隔

更新

我整天坐着6张椅子。因此,一个用户在2013年1月14日至2013年1月18日预订了3个字符,而另一个人在2013年1月17日至2013年1月20日预订了2个字符。所以我的预期输出如下所示。

ID Date         Available
1 2013-01-14     3
1 2013-01-15     3
1 2013-01-16     3
1 2013-01-17     5 
1 2013-01-18     5
1 2013-01-19     2
1 2013-01-20     2 
1 2013-01-21     2
阿比克·查克拉博蒂(Abhik Chakraborty)

怎么样

  select
  t.id,
  DATE_FORMAT(a.Date,'%Y-%m-%d') as Date
  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
  join test t on a.Date between t.Start_Date and t.End_Date
  order by t.id,Date

演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Java中查找两个日期之间的所有有效日期

显示两个日期之间的所有日期

如何在两个日期之间显示日期

如何生成两个日期之间的所有日期

如何获取两个日期之间的所有日期?

如何在laravel中接收两个日期之间的所有天数

如何在PHP Laravel中的两个日期之间从db获取所有数据?

如何在 Google 表格中的两个给定日期之间自动填充日期

如何显示两个日期之间的所有星期五日期

在php文件中显示两个给定日期之间的所有日期

在javascript中显示两个日期范围之间的所有日期

如何在Excel中的两个给定日期之间每2小时列出所有日期

不使用表格选择两个日期之间的所有日期(生成日期列表)

如何获取一周中给定的两个日期之间的所有日期?

如何获得两个日期之间的所有星期?

如何获得两个日期之间的所有月份?

如何显示给定两个日期之间的所有月份?

如何在android中获取两个日期之间的差额?尝试所有操作并发布

如何在swift4中连续获取两个日期之间的所有天数或列表?

如何在 SQLAlchemy 中生成两个日期之间的所有天数?

PrestoDB:选择两个日期之间的所有日期

打印两个日期之间的所有日期

如何在graphql中的两个日期之间查询

如何在php中的两个日期之间迭代?

SQL-显示两个日期之间的所有日期

如何在日期时间列上连接 postgresql 中的两个表,并在一个列中显示所有日期时间寄存器?

如何将两个日期之间的所有分钟添加到列表中

如何从两个日期之间堆叠的表中获取所有行?

显示两个日期之间的日期