scampisi Posted March 4, 2006 Share Posted March 4, 2006 I have a situation where I am needing to sort a list alphabetically by the last name. However, due to BAD planning, all of the names are stored in a single field like this[i]firstname lastname[/i]What is going to be my best way to sort my list? I looked through string manipulation in MYSQL, but I couldn't find a way to doa select statment that would search through my table, and pull all names based on the lastname.SELECT * from catalog ORDER BY . . . and here I want to say ORDER BY "the first character after the first space in " FULLNAMEAny ideas? Quote Link to comment https://forums.phpfreaks.com/topic/4117-order-by-the-second-word-in-a-field/ Share on other sites More sharing options...
SpectralDesign.Net Posted March 5, 2006 Share Posted March 5, 2006 I can't give you a straight answer, but I see that you can use regexp in MySQL... here's just one page I found, but it should be a good starting point....[a href=\"http://www.tech-recipes.com/mysql_tips484.html\" target=\"_blank\"]one page discussing regular expressions in MySQL selects[/a]I'd highly recommend exporting the data, and re-importing it into two tables though, because it'll save you time down the road when any similar situations arise. Quote Link to comment https://forums.phpfreaks.com/topic/4117-order-by-the-second-word-in-a-field/#findComment-14300 Share on other sites More sharing options...
wickning1 Posted March 5, 2006 Share Posted March 5, 2006 This will sort based on the second word (won't catch middle names correctly).[code]SELECT * from catalog ORDER BY SUBSTR(LTRIM(fullname), LOCATE(' ',LTRIM(fullname)))[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4117-order-by-the-second-word-in-a-field/#findComment-14305 Share on other sites More sharing options...
scampisi Posted March 6, 2006 Author Share Posted March 6, 2006 Thanks a million. That worked great. Quote Link to comment https://forums.phpfreaks.com/topic/4117-order-by-the-second-word-in-a-field/#findComment-14509 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.