Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql sort alphabetical and number

Tags:

mysql

I have an array that I want to sort alphabetically but also by the number at the end.

"SELECT DISTINCT Number FROM database WHERE 1 Order By Number ASC";

Here is how it currently sorts:

Number 1
Number 10
Number 11
Number 2
Number 3
Number 4
Number 5
Number 6
Number 7
Number 8
Number 9
The End

This is how I want it to sort:

Number 1
Number 2
Number 3
Number 4
Number 5
Number 6
Number 7
Number 8
Number 9
Number 10
Number 11
The End
like image 897
user1282355 Avatar asked May 06 '13 06:05

user1282355


1 Answers

Add another sort condition:

Order By LENGTH(Number), Number;

This works because a longer number is also a bigger number; for numbers of the same length, you can make a textual comparison, because '0' < '1' .... < '9'

like image 120
Ja͢ck Avatar answered Oct 25 '22 14:10

Ja͢ck