generate days from date range

后端 未结 29 2472
渐次进展
渐次进展 2020-11-21 05:19

I would like to run a query like

select ... as days where `date` is between \'2010-01-20\' and \'2010-01-24\'

And return data like:

29条回答
  •  心在旅途
    2020-11-21 06:14

    Procedure + temporary table:

    DELIMITER $$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `days`(IN dateStart DATE, IN dateEnd DATE)
    BEGIN
    
        CREATE TEMPORARY TABLE IF NOT EXISTS date_range (day DATE);
    
        WHILE dateStart <= dateEnd DO
          INSERT INTO date_range VALUES (dateStart);
          SET dateStart = DATE_ADD(dateStart, INTERVAL 1 DAY);
        END WHILE;
    
        SELECT * FROM date_range;
        DROP TEMPORARY TABLE IF EXISTS date_range;
    
    END
    

提交回复
热议问题