Wrong "order by" results

Posted on 2006-07-07
Last Modified: 2010-03-05
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):


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?

Question by:alosito

Expert Comment

ID: 17060146
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.
LVL 26

Accepted Solution

DireOrbAnt earned 100 total points
ID: 17065311
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 :)

Expert Comment

ID: 17073550
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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
collecting information 2 140
perl CPAN issue 3 99
Bash one liner, start and end time calculation 13 103
perl script to search for files containing text 3 73
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now