npsari Posted April 19, 2007 Share Posted April 19, 2007 I am using this code to retrieve names from the databse $q = "SELECT * FROM names ORDER BY date DESC LIMIT 5;"; How can i arrange names alphabetically Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/ Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 You already have them ordered by date. You could replace ORDER BY date DESC with ORDER BY name ASC, or whatever the title of the name column in your table is. Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233554 Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 just add names to your ORDER BY clause.. $q = " SELECT * FROM names, date ORDER BY names, date DESC LIMIT 5;"; Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233556 Share on other sites More sharing options...
per1os Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233559 Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 just add names to your ORDER BY clause.. $q = " SELECT * FROM names, date ORDER BY names, date DESC LIMIT 5;"; No point in having date in the order clause, since...either way it'll come out in pretty much the same order. Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233563 Share on other sites More sharing options...
npsari Posted April 19, 2007 Author Share Posted April 19, 2007 Ohh, thanks guys for all the support, I got it now Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233570 Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 No point in having date in the order clause, since...either way it'll come out in pretty much the same order. how so? Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233588 Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233589 Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233608 Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233610 Share on other sites More sharing options...
boo_lolly Posted April 19, 2007 Share Posted April 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/47807-how-can-i-arrange-results-alphabetically-in-mysql/#findComment-233618 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.