Ordering mySQL results by two fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Ordering mySQL results by two fields

Post by tomsace »

I am wanting to order games on my website in order of the highest rated. My database is something like this:

Game.....Rating.....Number of Votes
game1....4...........7
game2....5...........1
game3....2...........83
game4....5...........7

But if I order by rating game2 would come before game4 but I would like game4 to come before game2 because it is still rated 5/5 but has had more votes. I have tried a few different ways to do this but can't seem to figure it out? I am currently using "ORDER BY Rating DESC".

Thanks alot.
Tom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Ordering mySQL results by two fields

Post by requinix »

I guess you didn't know you can sort by multiple fields?

Code: Select all

...ORDER BY Rating DESC, Number of Votes DESC
It'll sort those rated best first, but of those with the same rating ones with more votes have precedence.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Ordering mySQL results by two fields

Post by tomsace »

Thanks alot got it working perfect.
Post Reply