Get value from the right set with 'ambiguous' fields

1

I'm doing the following query to my database:

  $escalacoes = 'SELECT * FROM escalacoes AS e JOIN jogador_rodada AS jr ON e.id_jogador = jr.id_jogador WHERE
    e.rodada = ('.$rodada_atual.' - 0) AND jr.rodada = '.$rodada_atual;
  $escalacoes = $pdo->query($escalacoes);
  $escalacoes->execute();
  while ($dadoEscalacao = $escalacoes->fetch(PDO::FETCH_ASSOC)){
    echo $dadoEscalacao['rodada'];
    echo "<br>";
  }
  • The "$ current_add" is defined elsewhere
  • That "- 0" is just because in my test I have to use it as
  • When I print in php the round, it takes the table table value (in this case, in> ). Being that I have a column with that same name in the other table ( 'player_rod' ). Is there a way to do this?

    Demo Image:

        
    asked by anonymous 08.07.2018 / 00:42

    1 answer

    1

    You can create a ALIAS for that field you want to redeem. That is, an alternative name for the field. So:

    $escalacoes = 'SELECT e.id, e.id_user, e.id_jogador, e.preco, e.status,
    jr.id_jogador AS id_jogador_direita, jr.id, jr.valor, jr.pontos, jr.status, jr.rodada
    FROM escalacoes AS e 
    JOIN jogador_rodada AS jr ON e.id_jogador = jr.id_jogador 
    WHERE
    e.rodada = ('.$rodada_atual.' - 0) AND jr.rodada = '.$rodada_atual;
    

    With this alias jr.id_jogador AS id_jogador_direita you will be able to select the desired field like this:

    $escalacoes = $pdo->query($escalacoes);
      $escalacoes->execute();
      while ($dadoEscalacao = $escalacoes->fetch(PDO::FETCH_ASSOC)){
        echo $dadoEscalacao['id_jogador_direita'];
        echo "<br>";
      }
    
        
    08.07.2018 / 02:01