MySQL Row to Column

前端 未结 1 2074
旧时难觅i
旧时难觅i 2020-12-28 22:04

can any one help me on how can I create a query output using the row data on the source table as a header on my output. Please see below for illustration.

E.G.

相关标签:
1条回答
  • 2020-12-28 22:24

    How about this??

    SELECT  
      GROUP_CONCAT(if(colHeader = 'Header 1', value, NULL)) AS 'Header 1', 
      GROUP_CONCAT(if(colHeader = 'Header 2', value, NULL)) AS 'Header 2', 
      GROUP_CONCAT(if(colHeader = 'Header 3', value, NULL)) AS 'Header 3' 
    FROM myTable; 
    

    Demo

    Note, you will need GROUP BY statement when there are more data of ids as shown below in Demo 2.

    Demo 2

    0 讨论(0)
提交回复
热议问题