如何在mysql工作台中将列转换为行

时间:2016-03-03 07:27:22

标签: mysql

我有一张这样的表:

enter image description here

但我看起来像这样。如何使用mysql workbench语法更改它?

enter image description here

1 个答案:

答案 0 :(得分:0)

 select max(case when id = 1 then col1 end) as `10`,
           max(case when id = 2 then col1 end) as `20`,
           max(case when id = 3 then col1 end) as `30`      
    from ((select id, `one`as col1, 1 as col2 from t1) union all
          (select id, two, 2 as col2 from t1) union all
          (select id, three, 3 as col2 from t1) union all
          (select id, four, 4 as col2 from t1) union all
          (select id, five, 5 as col2 from t1)
         ) temp
    group by col2;