Jump to content

How to "select"


MySQL_Narb

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/285074-how-to-select/
Share on other sites

"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
Link to comment
https://forums.phpfreaks.com/topic/285074-how-to-select/#findComment-1463770
Share on other sites

  On 1/4/2014 at 12:07 AM, Barand said:

 

"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!

Link to comment
https://forums.phpfreaks.com/topic/285074-how-to-select/#findComment-1463808
Share on other sites

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.