mysql - Row to column transform -


after multiple join, have raw results.

+----------------------------------------------------------------------+ | results                                                              | +----+----------+-------------+----------+-----------+-----------------+ | id | group_id | question_id | question | answer_id | answer | input  | +----+----------+-------------+----------+-----------+-----------------+ | 1  | 10001    | 1           | how old  | 1         | 25     | null   |   | 2  | 10001    | 2           | like| 3         | cola   | null   | | 3  | 10001    | 2           | like| 4         | other  | hotdog | | 4  | 10001    | 3           | city     | 5         | nyc    | null   | | 5  | 10001    | 4           | name     | 7         | other  | alex   | | 6  | 10002    | 1           | how old  | 1         | 25     | null   |  | 7  | 10002    | 2           | like| 6         | candy  | null   | | 8  | 10002    | 3           | city     | 8         | la     | null   |  | 9  | 10002    | 4           | name     | 7         | other  | roman  |  +----+----------+-------------+----------+-----------+--------+--------+ 

but want see in "one row view" group_id. such as:

+----+----------+-------------+----------+-----------+ | id | how old  |   | city     |  name     | +----+----------+-------------+----------+-----------+ | 1  | 25       | cola,hotdog | nyc      | alex      | | 2  | 25       | candy       | la       | roman     |   +----+----------+-------------+----------+-----------+ 

i don`t know normal group_by/concat construction that. must do?

set @i := 1; select @i := @i + 1 `id` , group_concat(case when question = 'how old' answer else null end) `how old` , group_concat(case when question = 'what like' if(answer='other', input, answer) else null end) `what like` , .... thetable group group_id ; 

group_concat ignores null values, returning null if values received null. case when else end statements use if(,,) used in "other" check; case more portable (ms sql server relatively added support kinds of ifs.)


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -