MySQL_Narb Posted January 3, 2014 Share Posted January 3, 2014 SELECT SUM(`money_earned`),SUM(`army_strength`),SUM(`worker_opm`) FROM `highscores` ORDER BY SUM(`money_earned`) DESC LIMIT $start,$per_page Above is my query pulling data from highscores. In the table highscores, there is a field called "group" to indicate the group they belong to. The purpose of this function is to get the total money of each group by getting the sum of all money from the member's submitted scores, and then ordering by `money_earned` DESC; Is there a way to do this? like get the sums of `money_earned` where all group numbers equal eachother e.g, it would get the total $$$ for everyone of group 4, total $$$ for everyone in group 3, etc Quote Link to comment https://forums.phpfreaks.com/topic/285074-how-to-select/ Share on other sites More sharing options...
Solution Barand Posted January 4, 2014 Solution Share Posted January 4, 2014 "group" is a poor choice for a column name as it is a MySQL reserved word. Whenever you use it in a query you must put it in backticks (`group`) What you need to do is GROUP BY `group` in you query. SELECT SUM(`money_earned`) as totEarn , SUM(`army_strength`) as totstrength , SUM(`worker_opm`) as totopm FROM `highscores` GROUP BY `group` ORDER BY SUM(`money_earned`) DESC LIMIT $start,$per_page Quote Link to comment https://forums.phpfreaks.com/topic/285074-how-to-select/#findComment-1463770 Share on other sites More sharing options...
MySQL_Narb Posted January 4, 2014 Author Share Posted January 4, 2014 "group" is a poor choice for a column name as it is a MySQL reserved word. Whenever you use it in a query you must put it in backticks (`group`) What you need to do is GROUP BY `group` in you query. SELECT SUM(`money_earned`) as totEarn , SUM(`army_strength`) as totstrength , SUM(`worker_opm`) as totopm FROM `highscores` GROUP BY `group` ORDER BY SUM(`money_earned`) DESC LIMIT $start,$per_page Wow, you are a life saver! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/285074-how-to-select/#findComment-1463808 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.