2017-05-31 85 views
0

獲取的開始和結束日期,我在我的數據庫中的兩個表:MySQL數據範圍從另一個表

日期

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

看看下面的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 
+0

所有我得到的是 – Seef

+0

你想說的話在當前日期後的第一個星期一的日期?有什麼問題嗎? –

+0

我希望它從日期表中獲取startdate和peakdate值,然後爲這兩個日期之間每週的星期一的日期創建一行。此列必須是Macrocycle表的一部分,並且也必須顯示其他字段。 – Seef

相關問題