我有三张桌子:
id_users)
在第三个表中,我将用户与中心连接起来,因为一个用户可以处理不同的中心。
通过这个选择,我得到了我想要的结果:
SELECT *
FROM `userInCenter`
RIGHT JOIN `user` ON `user`.`id_user` = `userInCenter`.`id_user`
";通过这个查询,我得到:

所以,这实际上是相当不错的,并且几乎实现了我想要的。
唯一的问题是,如果一个用户有两个中心,我的查询就会创建两行。
但是,我想要的是,显示一行,在相同的字段中显示中心: 8,7,像这样。
这个是可能的吗?还是我必须要改变什么?
小提琴: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;
?>提前谢谢亚历杭德罗。
发布于 2021-02-20 21:44:34
<?php
$con = mysqli_connect("localhost", "root", "", "gymtable_test");
//Retrieving the contents of the table
$res = mysqli_query($con, "SELECT ucd.id_usuarioCentroDeportivo, ucd.id_usuario, GROUP_CONCAT(ucd.id_centroDeportivo SEPARATOR ',') AS id_centroDeportivo, ucd.rolUsuarioCentro, ucd.createdAt, u.*
FROM usuarioCentroDeportivo ucd
RIGHT JOIN usuario u ON u.id_usuario = ucd.id_usuario
GROUP BY ucd.id_usuario");
//Fetching all the rows as objects
while ($row=mysqli_fetch_object($res)){
$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;
// do something with the variables
}
//Closing the statement
mysqli_free_result($res);
//Closing the connection
mysqli_close($con);按组将具有相同用户id (ucd.id_usuario)的行组合在一起,然后GROUP_CONCAT显示每个用户id的所有中心,用逗号分隔。
https://stackoverflow.com/questions/66295503
复制相似问题