Take value from right set with 'ambiguous' fields

Asked

Viewed 75 times

1

I am making 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>";
  }
  1. The "$rodada_actual" is set elsewhere
  2. That "-0" is just because in my test I have to use it like this

When I print in php the round, it takes the table value from left (in the case, the table 'escalations'). Since I have a column with the same name in the other table ('round'). Can I print the value of the right table in php itself?

Demonstrative image: inserir a descrição da imagem aqui

Obs: I know I don’t need this, since I’m determining the value of the round in the sql query, but I was wondering if it is possible to do this.

  • https://stackoverflow.com/questions/9122/select-all-columns-except-one-in-mysql take a look here, maybe select without counting the column with the same name q vc don’t want to solve

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 that alias jr.id_jogador AS id_jogador_direita you will be able to select the desired field so:

$escalacoes = $pdo->query($escalacoes);
  $escalacoes->execute();
  while ($dadoEscalacao = $escalacoes->fetch(PDO::FETCH_ASSOC)){
    echo $dadoEscalacao['id_jogador_direita'];
    echo "<br>";
  }
  • Thank you. That’s exactly it!

  • 1

    @VME TRANQUIL! =)

Browser other questions tagged

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