我的数据库中有两个表:
日期
id | startdate | peakdate
1 | 2017-07-03 | 2017-05-29
大环
id | date |
1 | 2017-07-03 |
2 | 2017-07-10 |
3 | 2017-07-17 |
我使用以下内容为两个日期之间的所有日期生成行:
select a.date
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) Week 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 '2017-07-03' and '2018-01-24'
如果我希望将'2017-01-20'和'2018-01-24'替换为从日期表中的startdate和peakdate读取的值,我该怎么办?我还需要显示在Macrocycle表中生成的日期,并在其旁边显示id,而不是像现在这样作为临时表。有什么想法吗?
答案 0 :(得分:0)
看下面的sql,也许会有所帮助。
select a.date
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) Week 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,Dates b
where a.date between b.startdate and b.peakdate