MySQL Row to Column

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.


Row Data:

| colHeader | value    |
| Header1   | value 1  |
| Header2   | value 2  |
| Header3   | value 3  |


| Header1   | header2   | Header3   |
| Value 1   | value 2   | Value 3   |

Is it possible??

Here is my MySQL script. I don’t think if is it the right way. Is there any idea on how could i arrive on the above output?

SELECT t1.value AS `Header1`,
       t2.value AS `Header2`,
       t3.value AS `Header3`
  FROM (SELECT * FROM table1 WHERE colHeader='Header1') t1
  JOIN (SELECT * FROM table1 WHERE colHeader='Header2'3) t2
  JOIN (SELECT * FROM table1 WHERE colHeader='Header3') t3;


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

How about this??

  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;


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

Demo 2

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x