php - Divide a table's column as different rows with mysql -


here's table: table1

  id | id_activity | id_elem | text  ---------------------------------------   1  |      1      |    11   | text1  |   2  |      1      |    12   | text2  |   3  |      1      |    13   | text3  |   4  |      2      |    11   | text4  |   5  |      2      |    12   | text5  |    6  |      2      |    13   | text6  |   7  |      3      |    11   | text7  |    8  |      3      |    12   | text8  |   9  |      3      |    13   | text9  |   10 |      4      |    11   | text10 |    11 |      4      |    12   | text11 |   12 |      4      |    13   | text12 |   13 |      5      |    11   | text13 |   14 |      5      |    12   | text14 |    15 |      5      |    13   | text15 |   16 |      6      |    11   | text16 |   17 |      6      |    12   | text17 |    18 |      6      |    13   | text18 |  

i need make result this:

  id | text_elem_11 | text_elem_12 | text_elem_13  ---------------------------------------------------   1  |    text1     |    text2     |    text3     |   2  |    text4     |    text5     |    text6     |   3  |    text7     |    text8     |    text9     |   4  |    text10    |    text11    |    text12    |   5  |    text13    |    text14    |    text15    |    6  |    text16    |    text17    |    text18    | 

what right way this? follow query can table first , second column

select table1.id,        table1.id_elem,        elem_11.text text_elem_11  table1  inner join table1 elem_11 on table1.id_activity = 1 ,  table1.id_elem = 11 ,  elem_11.id_elem = 11 

this result

  id_activity | id_elem  | text_elem_11 |  -----------------------------------------        1      |    11    |    text1     |        1      |    11    |    text4     |        1      |    11    |    text7     | 

i don't know how add others 2 columns , if idea single query... so, idea?

select  id_activity,         max(case when id_elem = 11 text end) text_elem_11,         max(case when id_elem = 12 text end) text_elem_12,         max(case when id_elem = 13 text end) text_elem_13    tablename group   id_activity order   id_activity 

output

╔═════════════╦══════════════╦══════════════╦══════════════╗ ║ id_activity ║ text_elem_11 ║ text_elem_12 ║ text_elem_13 ║ ╠═════════════╬══════════════╬══════════════╬══════════════╣ ║           1 ║ text1        ║ text2        ║ text3        ║ ║           2 ║ text4        ║ text5        ║ text6        ║ ║           3 ║ text7        ║ text8        ║ text9        ║ ║           4 ║ text10       ║ text11       ║ text12       ║ ║           5 ║ text13       ║ text14       ║ text15       ║ ║           6 ║ text16       ║ text17       ║ text18       ║ ╚═════════════╩══════════════╩══════════════╩══════════════╝ 

if have unknown number of id_elem, dynamic sql more preferred,

set @sql = null;  select  group_concat(distinct         concat('max(case when id_elem = ',                id_elem,                ' text else null end) ',                concat('`text_elem_' , id_elem, '`')                )) @sql tablename order id_elem;    set @sql = concat('select   id_activity, ', @sql, '                         tablename                     group   id_activity                     order   id_activity');  prepare stmt @sql; execute stmt; deallocate prepare stmt; 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -