Data display problem with INNER JOIN

Asked

Viewed 40 times

0

Good morning, I’m having a little problem in the data display with Inner Join, the query returned ok, but in the table view, instead of playing a row only, he is playing more than one, I would like the phone field, be as follows: phone1/phone2/phone3 in the way that there was only one line per user and in the phone grouped in this way, it is possible?

                         <?
                          $sql_usuarios = 'SELECT * FROM clientes AS tb1 INNER JOIN telefones AS tb2 ON(tb1.id_cliente=tb2.id_cliente_telefone) ORDER BY id_cliente ASC';
                          $executa = mysqli_query($conn, $sql_usuarios);
                          while ($dados = mysqli_fetch_assoc($executa)) {
                            $id_cliente = $dados['id_cliente'];
                            $nome_cliente = $dados['nome_cliente'];
                            $melhor_horario = $dados['melhorHorario_cliente'];
                            $telefones = $dados['telefone'];
                            if ($melhor_horario=='m') {
                              $melhor_horario = 'Período da Manhã';
                            }
                            elseif ($melhor_horario=='t') {
                              $melhor_horario = 'Período da Tarde';
                            }
                            else{
                              $melhor_horario = 'Período da Noite';
                            }
                        ?>
                        <tr>
                          <th scope="row"><?=$id_cliente;?></th>
                          <td><?=$nome_cliente;?></td>
                          <td><?=$telefones;?></td>
                          <td><?=$melhor_horario;?></td>
                        </tr> 
                        <?}?>
  • Search for the GROUP_CONCAT function by making a GROUP BY per user.

1 answer

1

If your phone chart has a "type" field, to record whether the phone is commercial, residential or mobile, and there is only one type of phone per customer, you could do this using subselects. Here is an example:

SELECT id_cliente, nome_cliente, melhorHorario_cliente, 
(select telefone from telefones where id_cliente_telefone = tb1.id_cliente and tipo ='RESIDENCIAL') as telefone1, 
(select telefone from telefones where id_cliente_telefone = tb1.id_cliente and tipo ='COMERCIAL') as telefone2, 
(select telefone from telefones where id_cliente_telefone = tb1.id_cliente and tipo ='CELULAR') as telefone3
FROM clientes AS tb1 ORDER by id_cliente ASC

The downside is that if there is too much data, it can make performance loss.

If there is no phone type field, another way to solve this would be to create a single column with all phones. Here is an example.

SELECT id_cliente, nome_cliente, melhorHorario_cliente, GROUP_CONCAT(tb2.telefone) as numeros_telefone FROM clientes AS tb1 INNER JOIN telefones AS tb2 ON(tb1.id_cliente=tb2.id_cliente_telefone) order by id_cliente ASC

Here is more information and examples:

https://www.geeksforgeeks.org/mysql-group_concat-function/

  • This last one didn’t work, at the time of rescuing it tells that the phone index does not exist

  • I set the second query by placing the table alias.

  • continues with error :( Notice: Undefined index: phone in C: xampp htdocs www Tony-official-changed displays-client.php on line 155 which in this case is when I try to recover the phone in $phones = $data['phone'];

  • solved, I did the following: $sql_usuarios = 'SELECT *, GROUP_CONCAT(tb2.phone) the numbers FROM clients AS tb1 INNER JOIN phones AS tb2 ON(tb1.id_client=tb2.id_cliente_phone) group by tb1.id_client order by id_client ASC';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.