0

I have a table with a list of members names and I want to sort them in alphabetical order. I have the members names as full names like Joe Bloggs, is there a way I can order them alphabetically by their last name.

I know its "select * from member ORDER BY name ASC";

2
  • ORDER BY name,lastname May 3, 2015 at 7:54
  • I don't have a last name the members name is just name.The members name is their first and last name together. I don't have firstname and lastname its just name.
    – hankey39
    May 3, 2015 at 7:56

1 Answer 1

3

There isn't a nice way to do it. This is why people typically store the last name in its own column.

You would have to create an algorithm to determine how to split the full name into constituent parts (i.e. perhaps you could split by space and take the last value, which would handle John Smith and John James Smith and John Smith-Fontleroy). i.e.

SUBSTRING_INDEX(SUBSTRING_INDEX(fullname, ' ', 3), ' ', -1) AS last_name

This is all quite inefficient, though, compared to sorting a column called lastName.

1
  • @amdixon sure - example added to back up my middle paragraph. The last sentence is the main point though.
    – Fenton
    May 3, 2015 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.