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? 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 ; 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 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
Archived
This topic is now archived and is closed to further replies.