Get a list of dates between two dates

后端 未结 20 2521
醉梦人生
醉梦人生 2020-11-22 00:26

Using standard mysql functions is there a way to write a query that will return a list of days between two dates.

eg given 2009-01-01 and 2009-01-13 it would return

20条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 00:59

    This solution is working with MySQL 5.0
    Create a table - mytable.
    The schema does not material. What matters is the number of rows in it.
    So, you can keep just one column of type INT with 10 rows, values - 1 to 10.

    SQL:

    set @tempDate=date('2011-07-01') - interval 1 day;
    select
    date(@tempDate := (date(@tempDate) + interval 1 day)) as theDate
    from mytable x,mytable y
    group by theDate
    having theDate <= '2011-07-31';
    

    Limitation: The maximum number of dates returned by above query will be
    (rows in mytable)*(rows in mytable) = 10*10 = 100.

    You can increase this range by changing form part in sql:
    from mytable x,mytable y, mytable z
    So, the range be 10*10*10 =1000 and so on.

提交回复
热议问题