user1076774 user1076774 - 9 days ago 5
MySQL Question

Running month logic in mysql

I have a table with two column col_a and col_b. I should have a value to it like in the below image. How can we get this.

enter image description here

Answer
set @month = 3;
select  t.month, s.month from
(
select 1 mns, 'Jan' month union select 2,'Feb' union select 3,'Mar' union
select 4,'Apr' union select 5,'May' union select 6,'Jun'  union
select 7,'Jul' union select 7,'Aug' union select 9,'Sep'  union
select 10,'Oct' union select 11,'Nov' union select 12,'Dec'
) s
,
(
select 1 mnt, 'Jan' month union select 2,'Feb' union select 3,'Mar' union
select 4,'Apr' union select 5,'May' union select 6,'Jun'  union
select 7,'Jul' union select 7,'Aug' union select 9,'Sep'  union
select 10,'Oct' union select 11,'Nov' union select 12,'Dec'
) t
where t.mnt <= s.mns    and t.mnt <= @month
Comments