Welcome to WebmasterWorld Guest from 54.235.1.148

Forum Moderators: open

Message Too Old, No Replies

Order Numbers in MySQL Table Largest First

Order By seems to only sort with smallest first

     
6:10 pm on Jan 14, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 12, 2005
posts:411
votes: 0


Hello,

I have a MySQL table with several columns, including one that contains numbers.

I can order this table and select the five lowest numbers using this command:

mysql_query("SELECT * FROM table ORDER BY number LIMIT 5");

However, say I want the five largest numbers, how do I do that?

In various searches I couldn't find anything about ordering numbers the opposite way, and in my various attempts I even tried with no avail:

mysql_query("SELECT * FROM table ORDER BY MAX(number) LIMIT 5");

Is there a way to do this?

Thank you.

6:34 pm on Jan 14, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 1, 2004
posts:607
votes: 0


"DESC" will reverse the sort order:

SELECT * FROM table ORDER BY number LIMIT 5 DESC
6:37 pm on Jan 14, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 12, 2005
posts:411
votes: 0


Ah-Ha!

Figured this one out:

mysql_query("SELECT * FROM table ORDER BY number DESC LIMIT 5");

I just needed to add DESC after what I wanted to by ordered.

4:23 am on Jan 15, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 12, 2005
posts:411
votes: 0


Crap!

Sorry zCat, thanks for the reply.

I didn't realize you had already replied when I typed my second message.

Glad we came up with the same answer!

Thanks.

9:25 am on Jan 15, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Oct 1, 2004
posts:607
votes: 0


No problem :-) (though I notice I put the "DESC" in the wrong position).
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members