Jump to content

GROUP BY and SORT BY causing issues


widget

Recommended Posts

Hi I am trying to return the results of a high scores table in php and mysql.

 

Sorting by score retrieves the following results.

 

joe 234

joe 213

jack 199

jill 198

jack 197

joe 145

 

I would like the results to sort by score and then group by username.

 

With my current code it shows the very first result in the database for the user no matter what the users score.

 

For example

 

Joe 213

Jill 198

Jack 197

 

Is there a way to do this?

 

$sql_query = "SELECT * FROM 'highscores_bubblepop' GROUP BY 'username' ORDER BY  'score' DESC LIMIT 0 , 50";
//store the SQL query in the result variable
$result = mysql_query($sql_query);

if(mysql_num_rows($result))
{
//output as long as there are still available fields
$x =1;

while($row = mysql_fetch_row($result))
{
$current_avatar = fetch("SELECT * FROM members_profiles2 WHERE username = '$row[1]'");
echo ("<div class=\"game_high_scores_content\" style=\"background-image: URL($base_url/images/avatars/$current_avatar[avatar].gif);\">$row[1] Scored $row[2]</div>");
$x ++;
}
}

//if no fields exist
else
{
echo "no values in the database";
} 
mysql_close($con);
?>

 

 

 

Link to comment
Share on other sites

order by score and username give the below results

 

fred Scored 111

fred Scored 115

joe Scored 234

lollypopchicka Scored 262

lollypopchicka Scored 269

joe Scored 777

 

$sql_query = "SELECT distinct `username`,`score` FROM `highscores_bubblepop` ORDER BY `score`,`username` DESC LIMIT 0 , 50";

Link to comment
Share on other sites

That kinda works

 

How would I then only include a players top score or 1 distinct entry?

 

Query

$sql_query = "SELECT distinct `username`,`score` FROM `highscores_bubblepop` ORDER BY `username`,`score` DESC LIMIT 0 , 50";

 

 

Results

 

fred Scored 115

fred Scored 111

joe Scored 777

joe Scored 234

lollypopchicka Scored 269

lollypopchicka Scored 262

 

On a personal note I just read your blog entry on selling your Mac, priceless!!

Link to comment
Share on other sites

so you just want the highest score of each user... ('total' holds the score value)

SELECT username,MAX(score) as total FROM highscores_bubblepop group by username LIMIT 0 , 50"

 

On a personal note I just read your blog entry on selling your Mac, priceless!!

p.s. they guy was asking for it. lol

Link to comment
Share on other sites

woot nvm i added ORDER BY `score` DESC

 

$sql_query = "SELECT username,MAX(score) as total FROM highscores_bubblepop group by username ORDER BY `score` DESC LIMIT 0 , 50";

 

joe Scored 777

lollypopchicka Scored 269

fred Scored 115

 

I could kiss you!!! thank you soo much I have been trying to fix this problem for years on and off.

 

 

Link to comment
Share on other sites

Hi me again!

 

I now have beta testers on the site and it seems its not working as it should after all :(

 

$sql_query = "SELECT username,MAX(score) as total FROM highscores_bubblepop group by username ORDER BY `score` DESC LIMIT 0 , 50";


//store the SQL query in the result variable
$result = mysql_query($sql_query);

if(mysql_num_rows($result))
{
//output as long as there are still available fields
$x =1;

while($row = mysql_fetch_row($result))
{
$current_avatar = fetch("SELECT * FROM members_profiles2 WHERE username = '$row[1]'");
echo ("<div class=\"game_high_scores_content\" style=\"background-image: URL($base_url/images/avatars/$current_avatar[avatar].gif);\">$row[0] Scored $row[1]</div>");
$x ++;
}
}

//if no fields exist
else
{
echo "no values in the database";
} 
mysql_close($con);

 

Result

 

2random4me Scored 2097

lollypopchicka Scored 1541

loulou2370 Scored 603

plutoheart Scored 1662

maryann1950 Scored 2126

twinkle Scored 386

faeryofavalon Scored 632

Link to comment
Share on other sites

I worked it out

 

$sql_query = "SELECT username,MAX(score) as total FROM highscores_bubblepop group by username ORDER BY `total` DESC LIMIT 0 , 10";

 

Had to order by total not score and it now works perfectly.

 

Hopefully this helps others as I know its taken me a long long time to find the answer :)

 

Thank you all who helped

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.