I have two tables:
comentarios
---id
---comentario
---usuario
---supermercado
---avaliacao
supermercado
---id
---nome
---endereco
---admin
I want to take the average rating of each supermarket and take the 3 supermarkets with the highest average.
Example:
Supermercado01
in the comments table has ratings:
4, 5, 3, 5 (média então é 4.25)
Supermercado02
in the comments table has ratings:
1, 1, 1, 1 (média então é 1)
Supermercado03
in the comments table has ratings:
4, 3, 3, 4 (média então é 3.5)
Supermercado04
in the comments table has ratings:
1, 5, 2, 2 (média então é 2.5)
SQL should then return me the records of Supermarket01, Supermarket3, and Supermarket4. Could it be done in a SQL only? I did not post any because the ones I tried were pretty flawed, and I was also trying to get the result in PHP, but the performance went awry.
The best attempt was:
SELECT supermercados.nome, AVG(comentarios.avaliacao) as avaliacao
FROM supermercados, comentarios
WHERE
supermercados.id = comentarios.supermercado ORDER BY avaliacao