MySQL order by string percentage

There is one field in MySQL database that stores the percentage value in raw form, for example: 303.3%. When we try to use usual ORDER BY clause, the result will not be correct. The field needs to be CAST-ed first into DECIMAL. An example of the query:

SELECT * from statistics ORDER BY CAST(`percentage` AS DECIMAL)DESC LIMIT 0,20

Leave a Reply