这应该是诀窍(MySQL方言,但很容易适应)
初始设置
SQL query: SELECT * FROM `test` LIMIT 0, 30 ; Rows: 3 start end 2008-01-01 2010-12-31 2009-01-01 2012-12-31 2009-01-01 2014-12-31
询问
SELECT `start` , min( `end` ) FROM ( SELECT t1.start, t2.end FROM test t1, test t2 WHERE t1.start < t2.end UNION SELECT t1.end + INTERVAL 1 DAY , t2.end FROM test t1, test t2 WHERE t1.end + INTERVAL 1 DAY < t2.end UNION SELECT t1.start, t2.start - INTERVAL 1 DAY FROM test t1, test t2 WHERE t1.start < t2.start - INTERVAL 1 DAY ) allRanges GROUP BY `start`
结果
start min( `end` ) 2008-01-01 2008-12-31 2009-01-01 2010-12-31 2011-01-01 2012-12-31 2013-01-01 2014-12-31