SELECT calculating note average in Mysql

Asked

Viewed 210 times

0

In a training system I have two tables:

TABLE nota_local:

id
id_local
nota

TABLE local:

id
nome

The table nota_local stores the notes that each user gives to the location where the training took place. When I list all the training locations, I give a select in the table local, but wanted to in addition to listing the location, say what the average grade that this site has, ie, give a select also in the table nota_local and already calculating the average, how to do?

  • SELECT l.nome, SUM(n.nota) / COUNT(n.nota) FROM locais as l INNER JOIN nota_locais as n ON n.id_local = locais.id works? If yes put an answer explaining

2 answers

1

Perform the following key joining operation and applying the AVG and GROUP BY functions:

SELECT
   nome,
   AVG(nota) AS 'media'
FROM locais L
INNER JOIN notas_locais NL ON
L.id = NL.id_local
GROUP BY 
   id,
   nome

1


Run the query below

select l.id, l.nome, AVG(nl.nota)
from nota_locais nl
inner join locais l  on  l.id = nl.id_local
group by l.id, l.nome
  • Thanks, I just didn’t give yours as certain because they answered before.

  • the previous answer does not answer your question as you are not using group by

  • True, tested and really did not group

Browser other questions tagged

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