user1973799
user1973799

Reputation: 43

Get the sum of the column in mysql

Below is the table I have created, my goal is to get the sum of the value depending of each person has, and exclude any username duplicate.

username | value
Bob          5
Vicky        10
Bob          12

Desire results:

username | value
Bob          17
Vicky        10

Upvotes: 2

Views: 465

Answers (4)

Anand Solanki
Anand Solanki

Reputation: 3425

Use the following query:

SELECT url, SUM(pos) FROM import_images GROUP BY url

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

This is what the GROUP BY clause do, use GROUP BY username with SUM(value):

SELECT username, SUM(value)
FROM tablename
GROUP BY username;

When you add a GROUP BY clause, rows that have the same values in the list of columns you specify in it, will be gathered into a group of unique values. For the other columns that are not listed in the GROUP BY clause, the aggregate function will be applied to it, the SUM(value) in the query above.

Upvotes: 8

ratnesh dwivedi
ratnesh dwivedi

Reputation: 352

Try This one

SELECT username, SUM(value) as val FROM tablename GROUP BY username;

Upvotes: 0

A. Zalonis
A. Zalonis

Reputation: 1609

Try this

SELECT T.username, SUM(T.value) AS value
FROM your_table_name T
WHERE T.username IS NOT NULL
GROUP BY T.username 

Upvotes: 0

Related Questions