Michdd Posted August 5, 2009 Share Posted August 5, 2009 I have a field which contains a name, the name may contain 2-3 names (first, middle, last).. I want to order the query by the last name alphabetically. Is there a way I can use like end(explode(' ', name)) inside the query. So it'll order by the last name? Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/ Share on other sites More sharing options...
jonsjava Posted August 5, 2009 Share Posted August 5, 2009 Add this to your query: <previous code> ORDER BY `last`; Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/#findComment-891594 Share on other sites More sharing options...
smerny Posted August 5, 2009 Share Posted August 5, 2009 jon, his 'name' is a single field with 2-3 words for the first middle last names  mich, you'd be better off creating 2-3 separate fields for the name, will be easier to work with (so you can do what jon suggested) Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/#findComment-891596 Share on other sites More sharing options...
jonsjava Posted August 5, 2009 Share Posted August 5, 2009 I didn't catch that. Yeah, create 3 separate fields, one for each part of the name. Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/#findComment-891597 Share on other sites More sharing options...
Michdd Posted August 5, 2009 Author Share Posted August 5, 2009 Is there really no other way? This isn't really an option in my case.. Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/#findComment-891610 Share on other sites More sharing options...
smerny Posted August 5, 2009 Share Posted August 5, 2009 if it's too late, you could create a script to explode the one field and enter it into the 3 Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/#findComment-891653 Share on other sites More sharing options...
Michdd Posted August 5, 2009 Author Share Posted August 5, 2009 That's not the reason this isn't plausible in my situation. Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/#findComment-891659 Share on other sites More sharing options...
fenway Posted August 10, 2009 Share Posted August 10, 2009 Is there really no other way? This isn't really an option in my case.. Why not? There are ugly sql ways of doing this, but you'd need to tell us more about every possible variation in your dataset. Quote Link to comment https://forums.phpfreaks.com/topic/168985-query-ordering/#findComment-894759 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.