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
https://forums.phpfreaks.com/topic/87917-solved-sum-of-column-where-username-john/
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

;

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.