Turn rows into columns.
Posted: Sat Apr 25, 2009 9:26 pm
Is there a way in mysql to turn rows into columns, or reverse/flip a table. I know this doesn't make much sense when I say this, but maybe you'll understand if you knew more about what I'm trying to do. Let's say I had a table with two rows: name and number. What I'm trying to do is turn the result set of a query to this table into a table with the same number of columns as there are rows and each column's name is the name field from the original table. The result set would contain only one row of integers from the number field of the original table. Let me illustrate a bit more:
Original table:
| name | number |
| Joe | 5551234 |
| John | 5554321 |
| Jim | 5555555 |
Let's say I had a table like the above with the three rows illustrated. I'm looking to return a result set based on this table like:
Result table:
| Joe | John | Jim |
| 5551234 | 5554321 | 5555555 |
What happens is each row in the original table get's a specific column in the result table with the name of a specific column from the original table and all the rest of the columns from the original table a row in the result table.
Is there a way to do the exact same thing in the example above?
Any help on this is very so much appreciated. Thanks for reading.
Original table:
| name | number |
| Joe | 5551234 |
| John | 5554321 |
| Jim | 5555555 |
Let's say I had a table like the above with the three rows illustrated. I'm looking to return a result set based on this table like:
Result table:
| Joe | John | Jim |
| 5551234 | 5554321 | 5555555 |
What happens is each row in the original table get's a specific column in the result table with the name of a specific column from the original table and all the rest of the columns from the original table a row in the result table.
Is there a way to do the exact same thing in the example above?
Any help on this is very so much appreciated. Thanks for reading.