Acchile
Acchile

Reputation: 1

What's is wrong with this WHERE Clause in MYSQL?

Please, I need help!

Don't matter where I put this clause: WHERE romaneios_detalhes.id_romaneio = '.$idr.' it's always returns a syntax error... I tried with and without the "table_name.", after and before all setences of my statment, with and without commas... nothing works and I am sure the solution is pretty simple...

What is the right place or right way to write this?

$sql3   =   'SELECT
         produtos_linhas.linha AS `COUNT(linha)`,
         produtos_tipos.tipo AS `COUNT(tipo)`, 
         COUNT(romaneios_detalhes.quantidade) AS `COUNT(quantidade)` 
         FROM romaneios_detalhes
         WHERE romaneios_detalhes.id_romaneio = '.$idr.'
         INNER JOIN produtos ON romaneios_detalhes.codigo = produtos.codigo
         INNER JOIN produtos_linhas ON produtos.id_linha = produtos_linhas.id
         INNER JOIN produtos_tipos ON produtos.id_tipo = produtos_tipos.id
         GROUP BY produtos_linhas.linha, produtos_tipos.tipo ';

echo '<p>'.$sql3.'</p>';
/* OUTPUT OF THIS ECHO:
SELECT produtos_linhas.linha AS `COUNT(linha)`, produtos_tipos.tipo AS `COUNT(tipo)`, COUNT(romaneios_detalhes.quantidade) AS `COUNT(quantidade)` FROM romaneios_detalhes WHERE romaneios_detalhes.id_romaneio = 3 INNER JOIN produtos ON romaneios_detalhes.codigo = produtos.codigo INNER JOIN produtos_linhas ON produtos.id_linha = produtos_linhas.id INNER JOIN produtos_tipos ON produtos.id_tipo = produtos_tipos.id GROUP BY produtos_linhas.linha, produtos_tipos.tipo
*/

$qry3   =   mysql_query($sql3) or die ($qry3_err.mysql_error());

Upvotes: 0

Views: 101

Answers (3)

Stian Soiland-Reyes
Stian Soiland-Reyes

Reputation: 192

Icarus and Brian are right, move WHERE below the JOINs.

Just a note, I hope $idr is not coming unprotected from a ?parameter or something, or you will be vulnerable for SQL injection.

Upvotes: 1

Icarus
Icarus

Reputation: 63956

The problem is that the INNER JOINS MUST go before the where clause!

You need to move the where clause at the end, before the group by.

Upvotes: 1

Brian Driscoll
Brian Driscoll

Reputation: 19635

The issue is that your WHERE clause is in the wrong place; it must be after your JOIN clauses.

Upvotes: 5

Related Questions