I have three tables:
- users (primary key: id_users)
- centers (primary key: id_centers)
- usersInCenters (primary key: id_usersInCenters, foreign keys: id_users and id_centers)
With the third table I connect the users with the centers, because 1 user can attend to different centers.
And with this SELECT I get the result that I want:
SELECT *
FROM `userInCenter`
RIGHT JOIN `user` ON `user`.`id_user` = `userInCenter`.`id_user`
";
With this query I get:
So, this is actually pretty good, and almost achieved what I want.
The only thing, is that my query creates two rows if one user has two centers.
But, what I want, is to display one row and show the centers in the same field: 8,7. Like this.
Is this possible? Or do I have to change something?
FIDDLE: http://sqlfiddle.com/#!9/0fed10/2
PHP:
<?php
while ($row=mysqli_fetch_object($listaDeUsuarios)){
$id_usuario=$row->id_usuario;
$nombreUsuario=$row->nombreUsuario;
$apellidoUsuario=$row->apellidoUsuario;
$emailUsuario=$row->emailUsuario;
$ciudadUsuario=$row->ciudadUsuario;
$createdAt=$row->createdAt;
$id_centroDeportivo=$row->id_centroDeportivo;
?>
Thanks in advance Alejandro.