MySQL varchars comparison

I have a problem when sorting desc / asc by column in a database that sorts it as follows.

9980 91 883 51 507 460 286 243 23 2131 

Now I realized that this is because I created colm varchar. In any case, so that it sorts correctly without changing the column to int?

thanks

+4
source share
2 answers

If you want the elements to be sorted as if they were numbers, cast their integer:

 ORDER BY CAST (field AS SIGNED INTEGER) 
+4
source

if you must:

 ORDER BY CAST(`YOUR_COL` AS SIGNED) 

for DESC

 ORDER BY CAST(`YOUR_COL` AS SIGNED) DESC 
+1
source

Source: https://habr.com/ru/post/1391314/


All Articles