gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:34 24 4
gpt4 key购买 nike

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

这是表格:

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-01-14 到 2013-01-18 之间预订了 3 个字符,另一个用户在 2013-01-17 到 2013-01-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

最佳答案

怎么样

  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

DEMO

关于mysql - 如何在表格中显示多个两个日期之间的所有日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com