[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

Wrong "order by" results

I have the following Perl code that takes the data from MySQL and sorts it:

$querystr = "select ID,Name,Description,PictureURL,InformationURL,Rate,10_Min,20_Min,Connect from $Table_Name order by 10_Min desc,Rate asc,Connect asc";
$sth = &sql($querystr);

For some reason the sorted results look like this (field 10_Min):

526
412
135
1342
125
95

Why is 1342 not on top of this list and what do I need to do to change in the code to have 1342 on top?



0
alosito
Asked:
alosito
1 Solution
 
Perl_DiverCommented:
it's ordered in ASCII order, not numeric order. But it's not perl, it's SQL that is ordering the data. I'm not sure of the proper SQL command though to order it numerically.
0
 
DireOrbAntCommented:
I'm not a MySQL guy, but try this:
$querystr = "select ID,Name,Description,PictureURL,InformationURL,Rate,10_Min,20_Min,Connect from $Table_Name order by CAST(10_Min AS UNSIGNED) desc,Rate asc,Connect asc";

If you have negative values, change UNSIGNED to SIGNED.

I hope this works :)
0
 
NorCal2612Commented:
This worked for me:

select ID, Name, Description, PictureURL, InformationURL, Rate, cast(10_Min as integer), 20_Min, Connect
from $Table_Name
order by 10_Min desc, Rate asc, Connect asc



0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now