Get a list of dates between two dates

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

    You can use MySQL's user variables like this:

    SET @num = -1;
    SELECT DATE_ADD( '2009-01-01', interval @num := @num+1 day) AS date_sequence, 
    your_table.* FROM your_table
    WHERE your_table.other_column IS NOT NULL
    HAVING DATE_ADD('2009-01-01', interval @num day) <= '2009-01-13'
    

    @num is -1 because you add to it the first time you use it. Also, you can't use "HAVING date_sequence" because that makes the user variable increment twice for each row.

提交回复
热议问题