How to sum all values of a column in Mysql?

Asked

Viewed 18,843 times

-2

How do I add all user points? For example:

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

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

  • 1

    SELECT SUM(PONTOS) AS total FROM tabela;

  • obgd, I will test

1 answer

9

How to do

Summing up:

SELECT SUM(PONTOS) AS total 
FROM tabela

Adding up 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 up together:

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

See working on Sqlfiddle


References

COUNT, SUM, AVG

GROUP BY

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

Browser other questions tagged

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