SYNTAX ERROR WHERE - PHP

Asked

Viewed 94 times

1

I have the following code in php:

$sql = "SELECT * FROM musicas_curtidas 
    GROUP BY musica 
    ORDER BY count(musica) 
    WHERE musica != 'Rádio Turn - Você em primeiro lugar!' AND
    WHERE musica != 'Radio Turn - Não importa o seu estilo! #2' AND
    WHERE musica != 'Radio Turn - Não importa o seu estilo!' AND
    WHERE musica != 'Rádio Turn - Comercial' AND
    WHERE musica != 'Rádio Turn - Na Balada'
    DESC LIMIT 3";    

You’re making a mistake for me:

You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'WHERE musica != 'Radio Turn - You first! ' AND ' at line 4

How can I fix this? I’m a beginner in php.

1 answer

5


First of all, PHP has nothing to do with the subject, this is an SQL error.

  • The Where clause is one
  • The Grouping then
  • The Order comes later
  • And the limit at the end

If you have any questions when mounting a query, the Manual is a good reference, even if it is not easy in English, have the examples and description of the syntax already in the right order:

https://dev.mysql.com/doc/refman/5.7/en/select.html

Probably could have improved the logic used, but from what he put in the question, these 4 points would be the first to solve.

SELECT *

FROM  musicas_curtidas 

WHERE musica != 'Rádio Turn - Você em primeiro lugar!'
      AND musica != 'Radio Turn - Não importa o seu estilo! #2'
      AND musica != 'Radio Turn - Não importa o seu estilo!'
      AND musica != 'Rádio Turn - Comercial' 
      AND musica != 'Rádio Turn - Na Balada'

GROUP BY musica 

ORDER BY count(musica) DESC

LIMIT 3

Could simplify to

SELECT *

FROM  musicas_curtidas 

WHERE musica NOT IN (
   'Rádio Turn - Você em primeiro lugar!',
   'Radio Turn - Não importa o seu estilo! #2',
   'Radio Turn - Não importa o seu estilo!',
   'Rádio Turn - Comercial',
   'Rádio Turn - Na Balada'
)

GROUP BY musica 

ORDER BY count(musica) DESC

LIMIT 3
  • @Paulosérgioperfect child, if you have any extra doubts, just comment (or if it’s something separate, open a new question)

  • Is it possible to add a WHERE to another column? For example, WHERE image NOT IN ('...'), both at the same time, music and image.

  • @Paulosérgiofilho can make the combinations you want, but with only one WHERE. Example: SELECT nota FROM notas WHERE nota IN ( "a", "b", "c") AND aluno_nome = "Roberto"; That is, will bring only when the name is Roberto and the note a, b or c

Browser other questions tagged

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