peterjc Posted September 10, 2007 Share Posted September 10, 2007 i have a sorting problem in php using the SQL command "ORDER BY". ----------------- For example: i want to sort this number descending: 30, 4, 50, 200. so in actual case we should get: 200 , 50, 30 , 4. But what i get when using the ORDER BY statement is: 50, 4, 30, 200 ------------------- After look a while at the incorrect result, i think the problem is because it sort using the first digit of the number. Anyone know how to solve this problem? Thank in advance. Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/ Share on other sites More sharing options...
AdRock Posted September 10, 2007 Share Posted September 10, 2007 Whatever field the numbers are in you need to so ORDER BY numbers DESC Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345101 Share on other sites More sharing options...
corbin Posted September 10, 2007 Share Posted September 10, 2007 What datatype is the field you're sorting? Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345103 Share on other sites More sharing options...
peterjc Posted September 10, 2007 Author Share Posted September 10, 2007 Oh. corbin, i use the char datatype for the number. i just realize it. At first i use float number datatype, but i change to char because when we total up the float number, there will be some inaccurate in php, am i correct? How to solve this? Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345109 Share on other sites More sharing options...
teng84 Posted September 10, 2007 Share Posted September 10, 2007 t At first i use float number datatype, but i change to char because when we total up the float number, there will be some inaccurate in php, am i correct? what are those? Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345122 Share on other sites More sharing options...
peterjc Posted September 10, 2007 Author Share Posted September 10, 2007 I i total or sum all the float value using SQL statement, the total result will not accurate. Just for an example: 10.6 + 12.8 = 23.4 but if you use SQL statement to sum it, it will output somethings like 23.3522222.(just for example). Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345163 Share on other sites More sharing options...
btherl Posted September 10, 2007 Share Posted September 10, 2007 To sort character column foo as an integer ORDER BY foo + 0 DESC ought to work.. You can also use CAST(foo AS UNSIGNED) Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345170 Share on other sites More sharing options...
peterjc Posted September 10, 2007 Author Share Posted September 10, 2007 hello btherl. i try to add the plus symbol(+) and zero(0) to the SQL statement and i work. But can u explain about it. And where to put CAST(foo AS UNSIGNED) Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345201 Share on other sites More sharing options...
btherl Posted September 11, 2007 Share Posted September 11, 2007 The other option is ORDER BY CAST(foo AS UNSIGNED) What happens when you add 0 to foo is that mysql is forced to convert foo into an integer. After that, it is sorted as an integer. The same happens when you explicitly cast foo to an integer using CAST(). "Cast" means "Change from one type to another". The original problem is that your numbers were being sorted as strings, which is why it looked at the first character first, then second, and so on. Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345676 Share on other sites More sharing options...
peterjc Posted September 11, 2007 Author Share Posted September 11, 2007 Thank for your explanation, btherl. I help me a lots. Quote Link to comment https://forums.phpfreaks.com/topic/68651-php-sorting-problem-usng-sql-order-by/#findComment-345726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.