Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Order by fails

Tags:

php

mysql

How can I solve this little problem

here is a screenshot:

enter image description here

and this is the query:

SELECT * 
FROM serverinfo
ORDER BY rank_pts DESC 

As you can see the order is incorect.

like image 505
Dave Avatar asked Dec 01 '25 05:12

Dave


2 Answers

You could try this:

ORDER BY CAST(rank_pts as float) DESC 
like image 74
MazepiC Avatar answered Dec 03 '25 21:12

MazepiC


Seems your ranl_pts column is varchar type so it's not sorting as numbers.

Use below trick.

  SELECT * 
  FROM serverinfo
  ORDER BY rank_pts+0 DESC 

OR

 SELECT * 
  FROM serverinfo
  ORDER BY CAST(rank_pts AS DOUBLE) desc

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!