How to convert a set of 4 rows to columns in MySQL? -
i have mysql question, , have no idea how resolve it.
i have following query:
select p.nombre, ti.tallerhorario_id persona p, talleresinscritos ti p.pasaporte = ti.pasaporte
at result this:
i'm looking make query in second column doesn't show row every "tallerhorario_id", every "nombre".
for example:
euclides | 7 | 24 | 32 | 48
liz lorena | 4 | 18 | 33 | 47
every person have 4 rows associated, without exceptions.
could me please?
thank you!
the simplest method put values in 1 column, using group_concat()
:
select p.nombre, group_concat(ti.tallerhorario_id) persona p join -- learn use proper explicit join syntax talleresinscritos ti on p.pasaporte = ti.pasaporte group p.nombre;
a comma-delimited list not asked for, might solve problem. if have counter, 1, 2, 3, , 4, second table, can using conditional aggregation:
select p.nombre, max(case when counter = 1 ti.tallerhorario_id end) id1, max(case when counter = 2 ti.tallerhorario_id end) id2, max(case when counter = 3 ti.tallerhorario_id end) id3, max(case when counter = 4 ti.tallerhorario_id end) id4 persona p join -- learn use proper explicit join syntax talleresinscritos ti on p.pasaporte = ti.pasaporte group p.nombre;
finally, if don't have counter, 1 way add 1 uses variables:
select p.nombre, max(case when counter = 1 ti.tallerhorario_id end) id1, max(case when counter = 2 ti.tallerhorario_id end) id2, max(case when counter = 3 ti.tallerhorario_id end) id3, max(case when counter = 4 ti.tallerhorario_id end) id4 persona p join -- learn use proper explicit join syntax (select ti.*, (@rn := if(@p = pasaporte, @rn + 1 if(@p := pasaporte, 1, 1) ) ) counter talleresinscritos ti cross join (select @p := '', @rn := 0) params order ti.pasaporte ) ti on p.pasaporte = ti.pasaporte group p.nombre;
Comments
Post a Comment