Helvin
Helvin

Reputation: 53

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.

Upvotes: 1

Views: 3950

Answers (1)

kyeiti
kyeiti

Reputation: 376

This should work:

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

Upvotes: 4

Related Questions