generate days from date range

后端 未结 29 2493
渐次进展
渐次进展 2020-11-21 05:19

I would like to run a query like

select ... as days where `date` is between \'2010-01-20\' and \'2010-01-24\'

And return data like:

29条回答
  •  滥情空心
    2020-11-21 06:03

    For anyone who wants this as a saved view (MySQL doesn't support nested select statements in views):

    create view zero_to_nine as
        select 0 as n 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;
    
    create view date_range as
        select curdate() - INTERVAL (a.n + (10 * b.n) + (100 * c.n)) DAY as date
        from zero_to_nine as a
        cross join zero_to_nine as b
        cross join zero_to_nine as c;
    

    You can then do

    select * from date_range
    

    to get

    date
    ---
    2017-06-06
    2017-06-05
    2017-06-04
    2017-06-03
    2017-06-02
    ...
    

提交回复
热议问题