Jump to content

output sum, field_name sorted


tom_b

Recommended Posts

ok, I have a simple table in my DB, looks something like this:

 

id    jim    bob    bill

1      20      30      15

2      20      35      15

 

etc,  what i need to do is get the sums for each column (I can do that), then output each field_name and the corresponding sum sorted desc like this

 

bob    65

jim      40

bill      30

 

I've tried a number of things but I'm stumped!!!  Any ideas?

 

Thanks, Tom

Link to comment
Share on other sites

You are trying to sort the columns?  SQL isn't designed for that.  Instead, you can do the sorting in php after fetching the data.

 

If you want to sort by name, then you should design your database like this:

 

score_id  name  score

1            jim      20

1            bob    30

1            bill      15

2            jim      20

2            bob    35

2            bill      15

 

Then just do

 

SELECT name, sum(score) FROM scores GROUP BY name ORDER BY sum(score) DESC

Link to comment
Share on other sites

Thanks, I'll give that a try.  I've been trying to do it with PHP, I can get the data O.K., just can't seem to get the field name and sum together in one row and then sort by the sum.  Maybe I can make it work this way, thanks for your time!!

 

Tom

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.