Mario.C
Mario.C

Reputation: 65

MySQL in PHP can't get values that are equal

I'm trying to make a top users of my website, but the problem that I'm having is if one or more users have the same amount of won games only the first result appears on the website.

Im using this code to search in the database:

$rs1 = mysql_query(SELECT won,steamid,name,avatar,games 
   FROM `users` 
   WHERE won <> 0 
   GROUP BY won DESC LIMIT 18); 
while($row = mysql_fetch_array($rs1))
{ //AND HTML CODE HERE...}

Can someone help me with this? I want to show all the users for example if they have the same number of won games, for example it would show like:

RANK - USER - WON
1    - NAME - 12
2    - NAME - 8
3    - NAME - 8
4    - NAME - 4

BTW I know that I should not use mysql_query but I can't do it another way.

Upvotes: 0

Views: 60

Answers (3)

Mahesh Madushanka
Mahesh Madushanka

Reputation: 3008

Add name in to Group by clause

SELECT won,steamid,name,avatar,games FROM users WHERE won <> 0 
GROUP BY won,name order by won,name DESC LIMIT 18

Upvotes: 0

svn
svn

Reputation: 1418

Seems to me that your GROUP BY should be an ORDER BY

rs1 = mysql_query(SELECT won,steamid,name,avatar,games 
               FROM `users` 
               WHERE won <> 0 
               ORDER BY won DESC LIMIT 18); 

If that does not work please share the table structure

Upvotes: 0

William Okano
William Okano

Reputation: 392

I don't think you need the group by clause, but if you do, please consider using all the projection fields on group by. Although mysql allows you to group by different fields, it's not a good practice to do so.

Upvotes: 1

Related Questions