gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:21:15 30 4
gpt4 key购买 nike

请看我的books 表:

书籍

+-----+----------+------------+------------+
| id | venue_id | from_date | to_date |
+-----+----------+------------+------------+
| 1 | 8 | 2015-07-21 | 2015-07-28 |
| 2 | 5 | 2015-08-03 | 2015-08-25 |
+-----+----------+------------+------------+

我想查看 from_dateto_date 之间的所有日期,其中 venue_id = 8

我的输出应该是:

Array
(
[0] => Array
(
[id] => 1
[venue_id] => 8
[today] => 2015-07-21
)
[1] => Array
(
[id] => 1
[venue_id] => 8
[today] => 2015-07-22
)
[2] => Array
(
[id] => 1
[venue_id] => 8
[today] => 2015-07-23
)
[3] => Array
(
[id] => 1
[venue_id] => 8
[today] => 2015-07-24
)
[4] => Array
(
[id] => 1
[venue_id] => 8
[today] => 2015-07-25
)
[5] => Array
(
[id] => 1
[venue_id] => 8
[today] => 2015-07-26
)
[6] => Array
(
[id] => 1
[venue_id] => 8
[today] => 2015-07-27
)
)

today 计算从 from_date 日期到 to_date 日期。但是我对那个查询是一片空白。如何编写 MySQL 查询以获得如上所示的输出?

最佳答案

我认为下面的查询应该获取结果。虽然我还没有测试查询。

select b.id,b.venue_id,d.date from books b 
join
(select a.Date, "8" as venue_id2
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (1000 * 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
where a.Date between '2010-01-20' and '2010-01-24') d on d.venue_id2 = b.venue_id and b.venue_id = 8

关于mysql - 如何获取两个日期之间的所有日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31530908/

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