Get a list of dates between two dates

后端 未结 20 2612
醉梦人生
醉梦人生 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:56

    Elegant solution using new recursive (Common Table Expressions) functionality in MariaDB >= 10.3 and MySQL >= 8.0.

    WITH RECURSIVE t as (
        select '2019-01-01' as dt
      UNION
        SELECT DATE_ADD(t.dt, INTERVAL 1 DAY) FROM t WHERE DATE_ADD(t.dt, INTERVAL 1 DAY) <= '2019-04-30'
    )
    select * FROM t;
    

    The above returns a table of dates between '2019-01-01' and '2019-04-30'.

提交回复
热议问题