MySQL in PHP can't get values that are equal

152

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 usemysql_query but I can't do it another way.

401

Answer

Solution:

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.

169

Answer

Solution:

Seems to me that yourGROUP BY should be anORDER 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

69

Answer

Solution:

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

People are also looking for solutions to the problem: javascript - infinite scroll from remote server with angular

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.