INNER JOIN with matching column conflict

Asked

Viewed 1,197 times

2

In this SELECT JOIN, all tables have columns with the same name. With this, it gives a conflict when listing.

Example: column Id_saida it is in all tables. How to list each of them?

$cmd = "SELECT f.*,a.*,e.*,p.*,m.*,v.* FROM a_finan AS f

       INNER JOIN agenda_saidas AS a
       ON a.id_saida    = f.id_saida

       LEFT JOIN empresas AS e
       ON e.id_empresa  = f.id_empresa

       LEFT JOIN passageiros AS p
       ON p.id_saida    = f.id_saida

       LEFT JOIN motoristas AS m
       ON m.id    = a.id_motorista

       LEFT JOIN veiculos AS v
       ON v.id_veiculo    = a.id_veiculo


       where a.id_transfer1 = '$id_transfer'  AND f.id_transfer =  
       '$id_transfer' 
       AND e.id_transfer  = '$id_transfer'  
       AND a.start BETWEEN '$de' AND '$ate' 
       ";

    $produtos = mysql_query($cmd);
    $total = mysql_num_rows($produtos);
    while ($linha = mysql_fetch_array($produtos)) {

     $id_saida = $linha['id_saida'];  // Coluna id_saida em todas as tabelas
     $id_saida = $linha['id_saida'];  // tabela 2
     $id_saida = $linha['id_saida'];  // tabela 3
     }
  • The ideal is always to select only the columns you need instead of using *, and in this case it is inevitable, because you have to add nicknames for each repeated column (as Silvio Andorinha reply) to be able to rescue them via php.

1 answer

6


Assign the field value to a reference for example in select use

SELECT p.id_saida as id_saidaTabelap, p.id_saida as id_saidaTabelaf

and when you go get the value do it

$id_saidaP = $linha['id_saidaTabelap'];  // tabela 2
$id_saidaF = $linha['id_saidaTabelaf'];  // tabela 3

Browser other questions tagged

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