phpmysqlpdo

Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous


I am making a simple query but it is not working and I don't know why. A query like this doesn't work :

$id = $_GET['id'];
$query = $connect->prepare("
SELECT N.id,N.titulo,N.texto,N.autor,N.data,J.imagem_noticia
    FROM noticias N 
    JOIN jogo J ON N.jogo_id = J.id 
    WHERE N.publicada =1 AND id=:numero");
$query->bindParam('numero',$id,PDO::PARAM_INT);
$query->execute();
$dados = $query->fetch(PDO::FETCH_ASSOC);

This is the error I get:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous


Solution

  • At the end of query id=:numero. id needs table alias. It should be N.id or J.id