Siggles Posted January 26, 2008 Share Posted January 26, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/87917-solved-sum-of-column-where-username-john/ Share on other sites More sharing options...
toplay Posted January 26, 2008 Share Posted January 26, 2008 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 ; Quote Link to comment https://forums.phpfreaks.com/topic/87917-solved-sum-of-column-where-username-john/#findComment-449851 Share on other sites More sharing options...
Siggles Posted January 26, 2008 Author Share Posted January 26, 2008 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/87917-solved-sum-of-column-where-username-john/#findComment-449944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.