Blease
Blease

Reputation: 1420

Add multiple MySQL entries together

I have a database with posts in it, to make it simpler, say I have two fields "user" and "rate". I want to select all of the ratings from all of the posts by user: "Joe" and add them together and display them. Then I want to do the same with the other users, to produce a leaderboard. I do not know the names of the users in the database.

Upvotes: 1

Views: 71

Answers (1)

John Woo
John Woo

Reputation: 263693

The query below produce only one result which is the totalrate for user JOE

SELECT SUM(Rate) totalRate
FROM tableName
WHERE user = 'joe'

this one will show the user with the greatest rate because it was sorted based on the totalRate in descending order

SELECT user, SUM(rate) totalRate
FROM tableName
-- WHERE user = 'JOE'
GROUP BY user
ORDER BY totalRate DESC

Upvotes: 3

Related Questions