我想获得两个日期之间的月份列表,例如2013-04-01和2014-03-07 输出应该是这样的
month
April
may
june
.
.
.
March
答案 0 :(得分: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');