Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL order by alphabet - numeric

Tags:

mysql

Is there any way to sort Elements in MySQL like this:

Elements:

  1. City 1
  2. City 2
  3. City 10
  4. City 1a

When i do the ORDER BY the list looks this way:

  1. City 1
  2. City 1a
  3. City 10
  4. City 2

Because all answer did not match to my special problem i edit my question. There are also Elements with Chars behind the numbers. Watch the example

like image 646
StrongLucky Avatar asked Feb 21 '23 17:02

StrongLucky


2 Answers

I use this and it works well:

ORDER BY LENGTH(Elements), Elements
like image 182
tommasop Avatar answered Mar 06 '23 19:03

tommasop


Try this trick - 'ORDER BY column_name+1', e.g.:

SELECT * FROM element
ORDER BY column_name+1;
like image 44
Devart Avatar answered Mar 06 '23 18:03

Devart