Rorschach Rorschach - 2 months ago 5
MySQL Question

Sort columns based on data in tuples mysql

Say, I have a table


A B C D E F
1 2 4 3 6 5
4 2 3 1 6 5
4 5 3 6 1 2




How can one get an output based on rearranging based on its data. For example,


ABDCFE
DBCAFE
EFCABD


is it possible?

EDIT:

The question seems to be asking: How can I get the list of column names in order by value?

Answer

I got it. You want to sort the values in each row and show the names of the columns in order.

Let me assume that you have a row id, so you can identify each row. Then:

select id, group_concat(which order by val) as ordered_column_names
from ((select id, a as val, 'A' as which from t) union all
      (select id, b, 'B' as which from t) union all
      (select id, c, 'C' as which from t) union all
      (select id, d, 'D' as which from t) union all
      (select id, e, 'E' as which from t) union all
      (select id, f, 'F' as which from t)
     ) t
group by id
order by id;