tags:

views:

41

answers:

3

How can I do a SELECT statement and have it returned the data incolumns instead of rows?

FROM

+----+-------+--------+
| id | name  | number |
+----+-------+--------+
|  0 | test  |     11 |
|  1 | test2 |     12 |
+----+-------+--------+

TO

+----+------+--------+----+-------+--------+
| id | name | number | id | name  | number |
+----+------+--------+----+-------+--------+
|  0 | test |     11 |  1 | test2 |     12 |
+----+------+--------+----+-------+--------+
+2  A: 

Look up 'pivot table' in the help files for your database engine.

Jay
+1  A: 

pobably you are searching this function:

mysql> SELECT student_name,
    ->     GROUP_CONCAT(test_score)
    ->     FROM student
    ->     GROUP BY student_name;

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Dobiatowski
fantastic! using group_concat i will actually be able to do what I want. thank you!
i'm glad :) ask if you want more info.
Dobiatowski
This is great thanks again. One minor issue with it though is I read in the docs it would ignore and not display null values with GROUP_CONCAT, are you aware a way to prevent this behavior to null values are shown?
So what you want to have instead of `NULL` values? empty string? - something like `1,2,3,,4,,5,6,,,7` ?
Dobiatowski
A: 

You really don't want to do that in SQL - I'm not even sure that any decent DBMS would allow such a thing!

Will A
@Mike but with MySQL you can only emulate the pivot. EDIT: uhmm, Mike disappeared.
Dobiatowski