你可以嘗試這樣的事情:
DATE '2006-05-20' + INTERVAL 5 DAY -> DATE '2006-05-25'
DATE '2006-05-20' + INTERVAL 5 MONTH -> DATE '2006-10-20'
DATE '2006-05-20' + INTERVAL 5 YEAR -> DATE '2011-05-20'
例1:
CREATE TABLE BEGIN_DATES
(
BDates DATETIME
);
INSERT INTO BEGIN_DATES VALUE ('13/08/2008');
INSERT INTO BEGIN_DATES VALUE ('13/02/2009');
INSERT INTO BEGIN_DATES VALUE ('13/08/2009');
INSERT INTO BEGIN_DATES VALUE ('15/02/2010');
INSERT INTO BEGIN_DATES VALUE ('13/08/2010');
INSERT INTO BEGIN_DATES VALUE ('14/02/2011');
INSERT INTO BEGIN_DATES VALUE ('15/08/2011');
INSERT INTO BEGIN_DATES VALUE ('13/02/2012');
INSERT INTO BEGIN_DATES VALUE ('13/08/2012');
INSERT INTO BEGIN_DATES VALUE ('13/02/2013');
INSERT INTO BEGIN_DATES VALUE ('13/08/2013');
INSERT INTO BEGIN_DATES VALUE ('13/02/2014');
INSERT INTO BEGIN_DATES VALUE ('13/08/2014');
INSERT INTO BEGIN_DATES VALUE ('13/02/2015');
SELECT BDates AS BEGIN, (BDates + INTERVAL 6 MONTH) AS END
FROM BEGIN_DATES
DROP TABLE BEGIN_DATES
Ë充足2:
BEGIN
SET @date = '2008-08-13';
SET @Edate ='2015-08-13';
WHILE @date <= @Edate DO
SELECT @date AS BEGIN_DATE, CAST(@date AS DATETIME) + INTERVAL 6 MONTH AS END_DATE; -- ADDDATE(@date,INTERVAL 6 MONTH)
SET @date = CAST(@date AS DATETIME) + INTERVAL 6 MONTH; -- Or ADDDATE(@date,INTERVAL 6 MONTH)
END WHILE;
END
謝謝。但是這意味着我必須知道開始日期是什麼,然後才能插入。我希望有一個更具動態性的方法,MySQL可以爲我制定六個月的日期是從X日期開始的。然後我必須將這個邏輯應用到一個包含超過11萬條不同日期X的記錄的表中。 – user1236443
您可以使用此方法採取動態方式:**選擇CURDATE()AS BEGIN,(CURDATE()+間隔6個月)** – Nation
你可以將它放在一個while循環 – Nation