Welcome to WebmasterWorld Guest from 54.198.87.238

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)

5+ Year Member



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)

10+ Year Member



"DESC" will reverse the sort order:

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

5+ Year Member



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)

5+ Year Member



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)

10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month