Skip to content
Advertisement

Sort MySQL query in PHP

I’m trying to sort different users ‘points’ by descending order (highest first). But at the moment the query is being returned in order of the user’s ID (order they appear in the database). I’m not sure what is wrong with my code?

The user can be in multiple leagues, so it first queries to see what leagues that the particular user is in. With the league IDs, I query to see what users are in each of the leagues. Then I query what each users total points are within that league. Ultimately, I want to get the rank of the user for each league but at the moment the query to order by the points is not working.

The image shows how the points are coming out. ‘1635’ is the users points that is logged in. For the first league, I’m trying to get ‘rank 2’ displayed.

enter image description here

Advertisement

Answer

You are trying to combine 2 tables: UserLeague and Points to select users from UserLeague and order them by Points. For such cases there is JOIN syntax in SQL:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement