Mysql queries to a column of numbers in descending order, the sort is not accurate, a small value in a row in front of a large value

Today time to sort the query data, found a small problem, that is sort of out of order is not correct. In doing this a record
statement is this SELECT * FROMthe Test ORDER BYTime desc ;
sort the results is wrong and finally through

show create table test;

Check out and found that it is of type varchar, not a numeric type.

Here Insert Picture Description

This also know the reasons, this is because your field type is text type, and type the text inside, '9'> '10', in text type is comparable from the left
and just need to modify a numeric type field or use query is converted to a number using +0, it can normally sorted
and I chose the latter

SELECT * FROM `test`  ORDER BY `time`+0 desc ;

Here Insert Picture Description

Published an original article · won praise 0 · Views 7

Guess you like

Origin blog.csdn.net/weixin_41408245/article/details/104835607