How to add everything in MYSQL

3

How do I add all of a table in MySQL for example ..

I have the table tb_comment , so I want to add in each post(id_mark) the amount of rate that will have the total.

For example, id_user 20 has the rate of 4 in id_mark 10 and id_user 21 has the rate of 5 in id_mark 10 and will give 9 to the total how do I select this in PDO?

    
asked by anonymous 11.12.2015 / 23:15

2 answers

6

MySQL has the SUM( ) function, suitable for your needs.

Here I am grouping by id_user, but can be by another field, it depends on the goal:

SELECT SUM(rate) AS total GROUP BY id_user

If you want to add everything, regardless of id_user, this is enough:

SELECT SUM(rate) AS total

In both cases, AS total is what gives the field name you will use to retrieve the result. Note that you must for a name that does not conflict with any column already in the table.

Update: According to your comment, if you want a user just enough:

SELECT SUM(rate) AS total WHERE id_user = (id do usuario desejado)

You do not even need GROUP BY in this case.

    
11.12.2015 / 23:32
1

Maybe this link will help: link .

Sum the value of a column

SELECT SUM(COLUNA) FROM TABELA 
    
11.12.2015 / 23:39