|
我有一个日期范围表:; f, @6 ]( O% `) X% G8 D
create table d date_start date, date_end date )insert into d values('2014-03-05','2014-04- insert into d values('2014-05-01','2014-06- insert into d values('2014-07-10','2014-08-15');我想在2014年填写日期不足的表格。在这种情况下,表格应该是:% u! f, i5 d9 L0 W& ~0 z+ T0 ]
2014-01-01 - 2014-03-04 2014-04-11 - 2014-04-30 2014-06-06 - 2014-07-09 2014-08-16 - 2014-12-31任何MySQL查询建议?! y/ d5 a. F" n+ {% B! W2 J
编辑. f' W! ]: k9 ?& F# R7 Z3 E( \
更好地使用这些值:
/ t& i$ W4 @2 a2 s create table d ( date_start date, date_end date )insert into d values('2014-06-01','2014-06-30);insert into d values('2014-07-01','2014-08-03'); insert into d values('2014-09-01','2014-09-30');编辑2
; x. M# j1 u: t" i我几乎可以做到这一点: \; C$ t! g& @5 k% y2 v/ Z, j
SELECT date_start, date_end FROM (SELECT ends.point AS date_start, starts.point AS date_end FROM (SELECT d.date_end INTERVAL 1 DAY AS point, @n:=@n 1 AS num FROM d CROSS JOIN (SELECT @n:=1) AS init0 ORDER BY date_start ) AS ends INNER JOIN (SELECT d.date_start-INTERVAL 1 DAY AS point, @m:=@m 1 AS num FROM d CROSS JOIN (SELECT @m:=0) AS init1 ORDER BY date_start ) AS starts ON ends.num=starts.num UNION ALL SELECT -01- MIN(date_start) - INTERVAL 1 DAY FROM d WHERE date_start>='2014-01-01' UNION ALL SELECT MAX(date_end) INTERVAL 1 DAY, -12- FROM d WHERE date_end 但对于以下时间间隔集,此查询返回错误结果:
' c( c5 n1 x1 q create table d (date_start date,date_end date); insert into d values('2014-01-01','2014-01- insert into d values('2014-01-10','2014-03- insert into d values('2014-03-16','2014-04- insert into d values('2014-05-01','2014-07- insert into d values('2014-08-01','2014-09- insert into d values('2014-12-25','2014-12-31');2014年似乎无法处理-07-31-2014-07-31这样的单日。- o% I) G0 i( s. n' t% w
" l5 \/ G2 {: k* @" K2 \! [: p
解决方案: |
|