Andreas
Andreas

Reputation: 121

PHP SQL group by user

I have a game where I'm saving each point to each player. Now I want to build a highscore where it lists the players with the best scores. but I want only the highest score per player visible.

Ex:

David 38 points
Elin 25 points
Kelly 3 points

And not:

David 38 points
David 35 points
Elin 25 points
Elin 23 points
Elin 20 points
etc etc

My code today:

$sql="select userID, poang from floppy ORDER BY poang DESC  LIMIT 10"; 
    $result=mysql_query($sql) or die(mysql_error()."<br />".$sql); 
    while($row = mysql_fetch_array($result)){
echo $row[userID]." ".$row['poang']." points<br />";
}

Anyone know how to do?

Upvotes: 0

Views: 67

Answers (2)

user3065100
user3065100

Reputation: 23

Use Group By For taking only one & max() for maximum value

Upvotes: 1

Oscar
Oscar

Reputation: 13960

SELECT TOP 10 userID, MAX(poang) from floppy GROUP BY userID ORDER BY MAX(poang) DESC

Upvotes: 3

Related Questions