Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove NULL cell in SELECT mysql?

I have a table YEAR like this

enter image description here

I want to select this table, so that I can get

enter image description here

I wonder if it is possible to get table where every cell is independent (not as one row/field).

like image 778
khusnanadia Avatar asked Jun 04 '26 06:06

khusnanadia


1 Answers

This is a real pain, but possible. The idea is to enumerate the values in each column and then "join" them together. The join is really a group by:

select max(`2014`) as `2014`,
       max(`2015`) as `2015`,
       . . .
from ((select (@rn2014 := @rn2014 + 1) as rn, `2014`,
              NULL as `2015`, NULL as `2016`, NULL as `2017`
       from year
       where `2014` is not null
      ) union all
      (select (@rn2015 := @rn2015 + 1) as rn, NULL, `2015`, NULL, NULL
       from year
       where `2015` is not null
      ) union all
      . . .
    ) y
group by rn;
like image 130
Gordon Linoff Avatar answered Jun 05 '26 18:06

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!