Jump to content

Trouble trying to sort table using ORDER BY


knz

Recommended Posts

Hi!

I got a problem with making my db-connected table so sort right. It's a table containing a highscore list for a trivia i have made. I want it to sort the table after, for example, the number of correct answers with the highest at the top.

 

I tried to fix this by using this code but without any luck:

 

$sql = "SELECT TableName FROM highscores GROUP BY TableName ORDER BY QuestionsCorrect";

 

I have tried both ASC and DESC.

 

Here is the whole code including the query: http://pastebin.com/m193ca5fa

 

Here is the sql-file exported from highscores: http://pastebin.com/m4e31b7fe

And here is a picture of what it looks like now: http://sv.tinypic.com/view.php?pic=2n824xi&s=5

 

The site may contain more than 1 trivia it's grouping the different games (TableName) so that the scored wont get in the same table. Im new with both php and mysql so im looking for the easiest solution as possible, and if you have som tips of changes please write them in a "for dummies" kind of way. Thank you!

 

// K

Link to comment
Share on other sites

I do a different method with doing a ranking system, see below.

 

SET @iCurUserRank=0

 

UPDATE users SET user_rank=(SELECT @iCurUserRank:=@iCurUserRank+1) ORDER BY user_score DESC, user_name ASC

 

That assigns the user_rank in order of the score, if two scores match then by their name. Then it's just a select statement to fetch the ranks. Something like this might be useful to you with some tweaking to how you want, just requires another column (or more if you want to do several).

Link to comment
Share on other sites

I do a different method with doing a ranking system, see below.

 

SET @iCurUserRank=0

 

UPDATE users SET user_rank=(SELECT @iCurUserRank:=@iCurUserRank+1) ORDER BY user_score DESC, user_name ASC

 

That assigns the user_rank in order of the score, if two scores match then by their name. Then it's just a select statement to fetch the ranks. Something like this might be useful to you with some tweaking to how you want, just requires another column (or more if you want to do several).

 

Hey thanks, but im not sure that I have the knowledge to make that procedure. I dont know what code should be changed and how?

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.