how to get a correct sequence in order by mysql?

Asked

Viewed 418 times

0

has a sequence of numbers 1 to 20 I would like to know if you have the correct number sequence for example:

1,2,3,4,5,6,7,8,9,10,11...

like this query select * from table order by numero asc;

he takes like this:

1,10,11,12,13,14,15,16,17,18,19,2,20...

has a way to get the first sequence I showed above using the order by

I tried to use a field for the order by didn’t work

  • 1

    The best thing would be to arrange the column for the right type. If it’s just numbers, it shouldn’t be CHAR/VARCHAR.

  • The column that has this sequence of numbers is in the numeric type?

2 answers

2

SELECT * FROM tabela ORDER BY CAST(coluna_numero AS unsigned) ASC

  • Uliano your tip worked out worth

1

There may be other ways to solve this, the more you can love the following way:

SELECT * FROM table ORDER BY LENGTH(id), id

Browser other questions tagged

You are not signed in. Login or sign up in order to post.