Jump to content

How to "select"


MySQL_Narb
Go to solution Solved by Barand,

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
Share on other sites

  • Solution

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

 

Wow, you are a life saver! Thank you!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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