0

我想获取两个日期之间的月份列表,例如 2013-04-01 和 2014-03-07 输出应该是这样的

month  
April
may
june
.
.
.
March
4

1 回答 1

1

我的建议很简单:

创建过程/函数

DELIMITER $$

USE `urlopy`$$

DROP PROCEDURE IF EXISTS `dowhile`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `dowhile`(dt_start DATE,dt_stop DATE)
BEGIN
  DECLARE v1 INT DEFAULT 0;

CREATE TEMPORARY TABLE tmp (
  `month_name` VARCHAR(256) DEFAULT NULL,
  `date_change` DATE DEFAULT NULL
) ENGINE=MYISAM DEFAULT CHARSET=utf8  ;  


  SELECT TIMESTAMPDIFF(MONTH, dt_start, dt_stop)+1  INTO v1;
  WHILE v1 > -1 DO
    INSERT tmp (SELECT MONTHNAME( ADDDATE(dt_start, INTERVAL v1 MONTH) ),ADDDATE(dt_start, INTERVAL v1 MONTH) );
    SET v1 = v1 - 1;
  END WHILE;

SELECT * FROM tmp;  
DROP TEMPORARY TABLE IF EXISTS tmp;  

    END$$

DELIMITER ;

然后调用它

CALL dowhile('2012-05-05', '2013-06-04');
于 2013-09-11T09:16:06.473 回答