8

How can I convert rows into columns in a MySQL query?

4
  • Depending upon what exactly you need you might need to use a PIVOT technique. In MySQL there is no inbuilt support so you need to use CASE statements. Can you provide a small example of source data and desired results? Commented Feb 9, 2011 at 10:26
  • for example , i am having a resultset with one col and 18 rows . how to convert them into 18 cols and 1 row Commented Feb 9, 2011 at 10:28
  • Right sounds like you are looking to transpose the whole thing. Pivot wouldn't do this. Commented Feb 9, 2011 at 10:29
  • This looks relevant, not sure if it's exactly a duplicate: stackoverflow.com/questions/1187460/sql-rows-to-columns Commented Feb 9, 2011 at 10:33

1 Answer 1

11

You can turn rows into a column with GROUP_CONCAT, but you can't transpose whole result sets in any automatic way. You either write a query that produces each column manually, or you do it in an application.

Here's a tutorial on writing the complicated queries to emulate the transposition:

http://www.artfulsoftware.com/infotree/queries.php#78

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.