如何在mysql中获取两个日期之间的月份列表

2021-11-20 00:00:00 sql mysql

我必须在 mysql 中获取两个日期之间的月份列表.

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.

推荐答案

SQLFiddle 演示

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

相关文章