generate days from date range

后端 未结 29 2409
渐次进展
渐次进展 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:12

    You'd like to get the a date range.

    In your example you'd like to get the dates between '2010-01-20' and '2010-01-24'

    possible solution:

     select date_add('2010-01-20', interval row day) from
     ( 
        SELECT @row := @row + 1 as row FROM 
        (select 0 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 6 union all select 7 union all select 8 union all select 9) t,
        (select 0 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 6 union all select 7 union all select 8 union all select 9) t2, 
        (select 0 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 6 union all select 7 union all select 8 union all select 9) t3, 
        (select 0 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 6 union all select 7 union all select 8 union all select 9) t4, 
        (SELECT @row:=-1) r
     ) sequence
     where date_add('2010-01-20', interval row day) <= '2010-01-24'
    

    Explanation

    MySQL has a date_add function so

    select date_add('2010-01-20', interval 1 day)
    

    will give you

    2010-01-21
    

    The datediff function would let you know often you'd have to repeat this

    select datediff('2010-01-24', '2010-01-20')
    

    which returns

     4
    

    Getting a list of dates in a date range boils down to creating a sequence of integer numbers see generate an integer sequence in MySQL

    The most upvoted answer here has taken a similar approach as https://stackoverflow.com/a/2652051/1497139 as a basis:

    SELECT @row := @row + 1 as row 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 @row:=0) r
    limit 4
    

    which will result in

    row
    1.0
    2.0
    3.0
    4.0
    

    The rows can now be used to create a list of dates from the given start date. To include the start date we start with row -1;

    select date_add('2010-01-20', interval row day) from
     ( 
        SELECT @row := @row + 1 as row 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 @row:=-1) r
     ) sequence
     where date_add('2010-01-20', interval row day) <= '2010-01-24'
    

提交回复
热议问题