ERROR SYNTAX WHERE - PHP

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";    

I'm getting an error:

  

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 music! =' Radio Turn - You in the first place! ' AND 'at line 4

How can I resolve this? I'm new to php.

    
asked by anonymous 10.01.2017 / 02:33

1 answer

5

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

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

If you are unsure when assembling a query , the Manual is a good reference, even if you do not have English easily, you have the syntax examples and description already in the right order:

  

link

I would probably have been able to improve the logic used, but from what I 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 for

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
    
10.01.2017 / 02:35