homepage Welcome to WebmasterWorld Guest from 54.205.197.66
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

    
Order Numbers in MySQL Table Largest First
Order By seems to only sort with smallest first
Jeremy_H




msg:3219103
 6:10 pm on Jan 14, 2007 (gmt 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.

 

zCat




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

"DESC" will reverse the sort order:

SELECT * FROM table ORDER BY number LIMIT 5 DESC

Jeremy_H




msg:3219117
 6:37 pm on Jan 14, 2007 (gmt 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.

Jeremy_H




msg:3219504
 4:23 am on Jan 15, 2007 (gmt 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.

zCat




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved