2016-04-14 16 views
1

私はこのような日付範囲の結果を生成するクエリ(BLA .. BLA .. SELECT * FROM)を持つ:MySQLは、日付範囲リストから各日付を生成します

code | date1 | date2 

a | 2016-04-19 | 2016-04-21 | 

b | 2016-04-13 | 2016-04-14 | 

が、私はそれの毎日を生成したいが次のようにdate1とdate2の間の日付範囲:

コード| date_result

a | 2016-04-19 

a | 2016-04-20 

a | 2016-04-21 

b | 2016-04-13 

b | 2016-04-14 

私はこのような2つの日付範囲の間の各日付を生産するクエリの例が見つかりました:

SELECT ADDDATE('2016-04-10', INTERVAL @i:[email protected]+1 DAY) AS DAY 
FROM (
SELECT a.a 
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 (SELECT @i := -1) r1 
WHERE 
@i < DATEDIFF('2016-04-19', '2016-04-10') 

をしかし、私は私のクエリでそれを実装することはできません:(

答えて

0

に動作します最終的に私は答えが発生することが判明日付1から日付2までの日付:あなたの感謝の連中

ため

select 
* 
from (
    select t.*, t.date1 + INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as tanggal 
    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 
    join (
     select date1,date2 from mytable 
    ) t 
    where t.date1 + INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY <= t.date2 
) a 

感謝

1

日付を変換することができますfrom_days() を使用して日番号に変換し、次に集計表を使用して内部結合を実行します。 番号730485は '2000-01-01'オフセットです(from from_days( '2000-01-01'))

ここ
select a.* , from_days(t.tallyid+730485) from 
(
    select 'a' code , '2016-04-19' date1, '2016-04-21' date2 
    union all 
    select 'b'code , '2016-04-13' date1, '2016-04-14' date2 
) a 
inner join Tally t on t.tallyid between (TO_DAYS(a.date1)-730485) and (TO_DAYS(a.date2)-730485) 
+0

興味深いですが、集計表のような一時表を作成したくない場合はどうしたらいいですか。 – Interardo

0

単一のクエリです:1〜100000

select a.* , from_days(t.tallyid+730485) from 
(
    select 'a' code , '2016-04-19' date1, '2016-04-21' date2 
    union all 
    select 'b'code , '2016-04-13' date1, '2016-04-14' date2, 
) a 
left join 
(
    SELECT @row := @row + 1 as tallyid FROM 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t, 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2, 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3, 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4, 
    (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t5, 
    (SELECT @row:=0) a 
) t on t.tallyid between (TO_DAYS(a.date1)-730485) and (TO_DAYS(a.date2)-730485) 

シーケンスがサブクエリ内に作成され、それは2000年と2237.の間の日付で