Get a list of dates between two dates

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

    DELIMITER $$  
    CREATE PROCEDURE popula_calendario_controle()
       BEGIN
          DECLARE a INT Default 0;
          DECLARE first_day_of_year DATE;
          set first_day_of_year = CONCAT(DATE_FORMAT(curdate(),'%Y'),'-01-01');
          one_by_one: LOOP
             IF dayofweek(affffdate(first_day_of_year,a)) <> 1 THEN
                INSERT INTO calendario.controle VALUES(null,150,affffdate(first_day_of_year,a),affffdate(first_day_of_year,a),1);
             END IF;
             SET a=a+1;
             IF a=365 THEN
                LEAVE one_by_one;
             END IF;
          END LOOP one_by_one;
    END $$
    

    this procedure will insert all dates from the beginning of the year till now, just substitue the days of the "start" and "end", and you are ready to go!

提交回复
热议问题