Page 1 of 1

Query with multiple ascending orders....

Posted: Mon Apr 16, 2007 2:13 am
by PhoenixTwo
Hi there, I am kind of a newbie to PHP and I have a question regarding ascending orders.

I am building a point system which is based through MYSQL. It is very similar to a sporting ladder, I will use the following example: name, goals against, goals for, and total points.

Now my question is this: I am able to sort the results of the query via the sortby ascending of the total points, that is fine. But I want to also be able to sort by "goals for" and "goals against" as a secondary sortby incase other teams have the same amount of points.

An example of what I mean is this:

Team goals for goals against total points
team1 3 3 3
team2 7 3 3
team3 10 2 3

So the database will read all three queries, and since the total points is the same, i am looking for a way where it will then sort by "goals for" and then in extreme circumstances, "goals against". So in theory, team3 should be on the top because of their "goals for" is higher than anyone else, and goals against is lower than everyone else.

I had a look around for something similar but to no luck. Maybe there is a simple way around this, although I must be looking in the wrong areas.

Any help regarding this issue will be very much appreciated.

Thanking you all in advance.

Posted: Mon Apr 16, 2007 2:51 am
by Christopher
You can put them all in the ORDER BY clause, like:

Code: Select all

SELECT * FROM pointsystem ORDER BY `goals against`, `goals for`, `total points`

Posted: Tue Apr 17, 2007 12:43 pm
by PhoenixTwo
Hi there,

Thank you so much, worked like a charm :D.

I also had a play around with it, and found that you can also use ASC and DESC on each individual ORDER BY option... Very useful!

Thank you so much once again... Very appreciated :)