Jump to content

How can I arrange results alphabetically in mySQL


npsari

Recommended Posts

Erhhmm..

 

We need column names to give you a proper query. You have only given us the table name.

 

$sql = "SELECT * FROM names ORDER BY name ASC LIMIT 5";

 

Note replace the "name" column with the column you want to do the alphabetical sorting with.

Well actually when I originally made that post I had read it thinking you put date before name, and so I had a post about that.  Then I edited it and didn't know what else to say...so...yea.

 

But anyway, by putting ORDER BY name ASC, date DESC, it won't even look at the date to order by unless two people with the same name come up.  If there are no two people with the same name, then it won't order by the date.

Well actually when I originally made that post I had read it thinking you put date before name, and so I had a post about that.  Then I edited it and didn't know what else to say...so...yea.

 

But anyway, by putting ORDER BY name ASC, date DESC, it won't even look at the date to order by unless two people with the same name come up.  If there are no two people with the same name, then it won't order by the date.

 

i was not aware of that. good to know. thanks for the tip bro!

Yea, the second ORDER BY in the clause is a fallback.  Basically, this

 

NAME                DATE

  Mark                345323

  Andrew            204284

  Mark                134533

 

Will become:

NAME                DATE

  Andrew            204284

  Mark                134533

  Mark                345323

 

There's no other way to really use another column to "sort" data, unless you're going to use it as a fallback.  Nothing else really makes sense.

wait, i already knew that! =) for some reason i was thinking about it differently than you explained it, but i'm on the same page. the second ORDER BY clause won't take effect unless there are more than one of the same field values for the first ORDER BY clause. that makes total sense.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.