Helvin Helvin - 1 year ago 53
SQL Question

MySQL How to order by last name on a full name field?

I have a SQL table with a column called full name which contains "John Doe"

How can order the data by the last name that appears in the full name column ?

For a long name like "John Doe Second", I would like to order the data by the word "Doe".

I am using MySQL database.

EDIT: Here is the precision : Case "John" is impossible. What I consider as the last name should be after the FIRST space in my string.

Answer Source

This should work:

SELECT * FROM some_table ORDER BY SUBSTR(Name, INSTR(Name, ' '))