I hve to get the list of months between two dates in mysql.
For Example:My Input is
From date 23-01-2013
To Date 01-04-2014
Output Should be
Jan 2013,
Feb 2013,
March 2013,
.
.
.
Jan 2014,
Feb 2014,
Mar 2014,
Apr 2014.
解决方案select
DATE_FORMAT(m1, '%b %Y')
from
(
select
('2013-01-23' - INTERVAL DAYOFMONTH('2013-01-23')-1 DAY)
+INTERVAL m MONTH as m1
from
(
select @rownum:=@rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select @rownum:=-1) t0
) d1
) d2
where m1<='2014-04-01'
order by m1
文章描述了如何使用SQL查询在MySQL中获取从指定开始日期(如2013-01-23)到结束日期(如2014-04-01)之间的逐月列表,通过DATE_FORMAT和INTERVALDAYOFMONTH函数实现。
5189

被折叠的 条评论
为什么被折叠?



