Count function - Mysql

Asked

Viewed 151 times

1

I have the following tables:

times
- time_id INT
- time_nome VARCHAR

rodadas
- rod_id INT
- rod_rodada INT 
- rod_pontos DECIMAL (10,2)
- rod_fk_time INT

I need to add the score and group by team.

2 answers

4


Actually to add up the score you need the function SUM, and not the function COUNT. The result would be something like this:

SELECT tim.time_nome,
       SUM(rod.rod_pontos) AS total
  FROM times tim
       INNER JOIN rodadas rod ON rod.rod_fk_time = tim.time_id
 GROUP BY tim.time_nome
 ORDER BY 2 DESC,
       tim.time_nome

In the query above the function SUM will add up all values, separating them by what is determined in the grouper clause GROUP BY. Therefore, all wheel points separated by time_nome shall be summed and presented. In the clause ORDER BY put the number 2, indicating that the second column of the query shall be used in descending order (in the case of the total score).

-2

I managed to solve.

SELECT t.time_nome, SUM(r.rod_pontos) PONTOS
FROM rodadas r
INNER JOIN times t
WHERE t.time_id = r.rod_fk_time
GROUP BY r.rod_time
ORDER BY SUM(r.rod_pontos) DESC
  • I believe that it will not work for some reasons. Before the table rodadas does not have a column rod_time as specified in its where. According to which column rod_pontos is not inside an aggregator function and appears in the field relation. Third you are using column information rod_pontos doing nothing with her.

  • Now you must be right hehe;

Browser other questions tagged

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