MySQL Forums
Forum List  »  PHP

SQL - rows to columns
Posted by: Luis Guti
Date: February 02, 2019 11:24PM

i have 3 tables

|| user ||
||------------------------------------||
|| name || lastname || documentoU ||
|| Andres || Perez || 123456 ||



|| user_info_user ||
||-----------------------------------------------||
|| id || documentoU || idInfo || infoValor ||
|| 1 || 123456 || 1 || 123456789 ||
|| 2 || 123456 || 1 || 987654321 ||
|| 3 || 123456 || 2 || email@email.com ||


|| info_user ||
||----------------------||
|| idInfo || name ||
|| 1 || phone ||
|| 2 || email ||

El resultado de usuario_info_usuario es

|| documentoU || name || lastname || infoValor ||
||---------------------------------------------------||
|| 123456 || Andres || Perez || 123456789 ||
|| 123456 || Andres || Perez || 987654321 ||
|| 123456 || Andres || Perez ||email@email.com||

I need it to be like that

|| documentoU || name || lastname || a || b || c ||
||------------------------------------------------------------------------||
|| 123456 || Andres || Perez ||123456789||987654321||cor@correo.com||

to get the values of the rows in PHP

if I use group by, only the first row remains

i have its SQL:

SELECT u.num_documento_usuario,
u.nombre,
u.apellido,
u.tipo_documento,
coalesce((case when i.id_info_usuario = '2' then i.info_usuario_valor end), NULL) as A,
coalesce((case when i.id_info_usuario = '11' then i.info_usuario_valor end), NULL) as B
FROM usuario AS u
INNER JOIN usuario_info_usuario AS i
ON i.num_documento_usuario = u.num_documento_usuario
INNER JOIN info_usuario AS inf ON inf.id_info_usuario=i.id_info_usuario
WHERE inf.nombre_info IN ('tel_celular' ,'correo')
GROUP BY u.num_documento_usuario, u.nombre, u.apellido, u.tipo_documento

tk,

Options: ReplyQuote


Subject
Written By
Posted
SQL - rows to columns
February 02, 2019 11:24PM
February 02, 2019 11:41PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.