How to get a list of months between two dates in mysql

后端 未结 3 2040
萌比男神i
萌比男神i 2020-11-30 06:00

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   20         


        
3条回答
  •  情深已故
    2020-11-30 06:32

    SQLFiddle demo

    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
    

提交回复
热议问题