2012-05-23 187 views
0

之間週期以我的表,我有兩個單獨的日期字段,X和Y在字段×1具有13/08/2008和外地YI的日期有日期13/08/2015年。MySQL查詢顯示日期

我在想,如果有工作了六個月時間日期是現場X和Y之間有什麼SQL的方式,其在單獨的行顯示的結果?所以,我希望在兩個領域有以下結果:

BEGIN  | END 
13-Aug-08 | 13-Feb-09 
13-Feb-09 | 13-Aug-09 
13-Aug-09 | 15-Feb-10 
15-Feb-10 | 13-Aug-10 
13-Aug-10 | 14-Feb-11 
14-Feb-11 | 15-Aug-11 
15-Aug-11 | 13-Feb-12 
13-Feb-12 | 13-Aug-12 
13-Aug-12 | 13-Feb-13 
13-Feb-13 | 13-Aug-13 
13-Aug-13 | 13-Feb-14 
13-Feb-14 | 13-Aug-14 
13-Aug-14 | 13-Feb-15 
13-Feb-15 | 13-Aug-15 

這可能嗎?

感謝

回答

0

你可以嘗試這樣的事情:

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 
+0

謝謝。但是這意味着我必須知道開始日期是什麼,然後才能插入。我希望有一個更具動態性的方法,MySQL可以爲我制定六個月的日期是從X日期開始的。然後我必須將這個邏輯應用到一個包含超過11萬條不同日期X的記錄的表中。 – user1236443

+0

您可以使用此方法採取動態方式:**選擇CURDATE()AS BEGIN,(CURDATE()+間隔6個月)** – Nation

+0

你可以將它放在一個while循環 – Nation