Andreas Baran
Andreas Baran

Reputation: 659

Mysql how to sort by firstletter in first name and first letter in lastname

I have a list with names like Anders Mansen, Andreas Burne. When I order by firstname, lastname it show up like this:

Anders Mansen
Andreas Burne

How can i make the search so it would take the first letter in firstname and first in last, so the list will be like this

Andreas Burne
Anders Mansen

Hope someone can help:-D

Andreas

Upvotes: 1

Views: 2636

Answers (2)

jpw
jpw

Reputation: 44881

This would do what you want:

select * from your_table order by left(firstname,1), left(lastname,1)

Although I would think that ordering by lastname, firstname might make more sense as sorting by a single character in firstname and lastname will give rather unpredictable results for names that have the same initial letter but differ in the rest:

select * from your_table order by lastname, firstname;

Upvotes: 2

Beartums
Beartums

Reputation: 1350

select firstname,lastname
from tablename
order by left(firstname,1), left(lastname,1)

Depending on the size of the table and how it is indexed, this could be a slow sort

Also, this makes no determination about anything other than initials, so there is no telling what order 'Anders Burne' and 'Anders Black' will show up in.

Hope this helps.

Upvotes: 0

Related Questions