How to order by the largest quantity

I have a pretty simple question, I think ..

I have a database field that stores the size of the byte files. I want to order the largest file_file, but it does not work as I hoped ...

I tried

SELECT * FROM file_data ORDER BY file_size DESC 

and

 SELECT * FROM file_data ORDER BY MAX( file_size ) DESC 

Thanks.

EDIT:

The reason it does not work as expected is because these are the results of the first query:

 ID file_size 13 980616 200 9782897 199 9732752 187 967006 166 9614688 12 9481028 44 945120 65 9228440 208 92140357 
+4
source share
2 answers

if file_size is a CHAR / VARCHAR field, probably the result is not the one you expected (edit: in support of my thesis, the results you posted seem to be sorted alphabetically)

So try changing the column data type to INTEGER or, if you cannot change the table, do explicit casting in your query using

 SELECT * FROM file_data ORDER BY CAST(file_size AS INTEGER) DESC 

see http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html

+6
source

CAST or CONVERT should solve your problem

 SELECT * FROM file_data ORDER BY CONVERT(file_size,INTEGER) DESC 
+3
source

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


All Articles