How to sum all the values of a column in MySQL? [duplicate]

-3

How do I add up all user points? For example:

ID | NOME  |  PONTOS
 1 | Joao  |   100
 2 | Bia   |    50 

It is known that the sum of all points in the database is 150.

    
asked by anonymous 02.09.2018 / 22:48

1 answer

4

How to do

Summing up:

SELECT SUM(PONTOS) AS total 
FROM tabela

Adding by users:

SELECT NOME, SUM(PONTOS) AS total 
FROM tabela
GROUP BY NOME

Counting the amount of records per user:

SELECT NOME, COUNT(PONTOS) AS registros
FROM tabela
GROUP BY NOME

Displaying the average per user:

SELECT NOME, AVG(PONTOS) AS media
FROM tabela
GROUP BY NOME

All together above:

SELECT NOME, SUM(PONTOS) AS total, COUNT(PONTOS) AS registros, AVG(PONTOS) AS media
FROM tabela
GROUP BY NOME

See working in SQLFiddle

References

COUNT, SUM, AVG

GROUP BY

DISTINCT and GROUP BY, what's the difference between the two statements?

    
02.09.2018 / 23:27