Get a list of dates between two dates

后端 未结 20 2155
醉梦人生
醉梦人生 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 01:04

    You can use this

    SELECT CAST(cal.date_list AS DATE) day_year
    FROM (
      SELECT SUBDATE('2019-01-01', INTERVAL 1 YEAR) + INTERVAL xc DAY AS date_list
      FROM (
            SELECT @xi:=@xi+1 as xc from
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc1,
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc2,
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc3,
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc4,
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc5,
            (SELECT @xi:=-1) xc0
        ) xxc1
    ) cal
    WHERE cal.date_list BETWEEN '2019-01-01' AND '2019-12-31'
    ORDER BY cal.date_list DESC;
    

提交回复
热议问题