Jump to content

[SOLVED] Sum of column WHERE username = John


Siggles

Recommended Posts

Hi,

 

I am trying to find the correct way of getting the total score from a column..

 

For example..

 

Username  Score

Richard      12

Sam          11

Richard      22

 

I would want to get  Richard's total score. Eg, 34.

 

I have looked at SUM() but that does not allow WHERE clause.

 

Can you help?

Link to comment
Share on other sites

It does allow a "WHERE" clause.

 

Here are various examples:

 

# This gets ALL rows and summarizes them by "Username" column

 

SELECT `Username`, SUM(`Score`) AS Total_Score

FROM table_name

GROUP BY `Username`

;

 

 

# This gets just rows for username "Richard" and summarizes them

# group by maybe optional here

 

SELECT `Username`, SUM(`Score`) AS Total_Score

FROM table_name

WHERE `Username` = 'Richard'

GROUP BY `Username`

;

 

 

# This gets all rows and summarizes them by "Username" column but only

# returns the ones that have a total score of 16 or more (like "Richard")

 

SELECT `Username`, SUM(`Score`) AS Total_Score

FROM table_name

GROUP BY `Username`

HAVING Total_Score > 15

;

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.