473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

MySQL sort

Hi, may be some of you MySQL database guru can shed me some lite to do the
following:

I have rows of data in mysql as follow:

CODES
=====
HH-1
....
HH-1000

When I select the CODE column, instead of getting HH-1,HH-2, HH-3....
it prints HH-1xxx first then HH-2xxx ...
It seems it treats them just as text.

How do I sort it and came out what it should be, HH-1, HH-3, ...HH-21,
,HH-22,...,HH-100, HH-101...etc

Or do I have to make sure that it has 4digit with 0's padding e.g
HH-1-> HH-0001, H-2->HH-0002, HH-156-> HH-0156...

If anyone know that there are an easier way without touching/modifying
what's in the database, I'd appreciate the advice. Thanks

Jul 17 '05 #1
2 2461
Ruby Tuesday:
Hi, may be some of you MySQL database guru can shed me some lite to do the
following:

I have rows of data in mysql as follow:

CODES
=====
HH-1
...
HH-1000

When I select the CODE column, instead of getting HH-1,HH-2, HH-3....
it prints HH-1xxx first then HH-2xxx ...
It seems it treats them just as text.

How do I sort it and came out what it should be, HH-1, HH-3, ...HH-21,
,HH-22,...,HH-100, HH-101...etc

Or do I have to make sure that it has 4digit with 0's padding e.g
HH-1-> HH-0001, H-2->HH-0002, HH-156-> HH-0156...

If anyone know that there are an easier way without touching/modifying
what's in the database, I'd appreciate the advice. Thanks


There really isn't, as you have reasoned, MySQL treats the data as text. You
must either ensure that the codes contain four digits, or introduce a sort
column, or you can split the codes into two parts, one textual and one
numeric.

André Næss
Jul 17 '05 #2
On Mon, 16 Feb 2004 01:45:53 -0500, "Ruby Tuesday"
<ru**********@yahoo.com> wrote:
Hi, may be some of you MySQL database guru can shed me some lite to do the
following:

I have rows of data in mysql as follow:

CODES
=====
HH-1
...
HH-1000

When I select the CODE column, instead of getting HH-1,HH-2, HH-3....
it prints HH-1xxx first then HH-2xxx ...
It seems it treats them just as text.

How do I sort it and came out what it should be, HH-1, HH-3, ...HH-21,
,HH-22,...,HH-100, HH-101...etc

Or do I have to make sure that it has 4digit with 0's padding e.g
HH-1-> HH-0001, H-2->HH-0002, HH-156-> HH-0156...

If anyone know that there are an easier way without touching/modifying
what's in the database, I'd appreciate the advice. Thanks


You can hardly be surprised if mySQL treats varchar columns as text!

I had a similar problem when dealing with some legacy data. I used
something akin to:

.... order by cast( (substring(code from 4)) as unsigned )

My database was MS SQL Server, so the above is untested for mySQL. You
can certainly order by substrings, though. It'll be quite slow as that
operation must be performed for every row.

Andre's solutions are proper solutions to the problem, though.

--
David ( @priz.co.uk )
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roger Lord | last post by:
From a html file I have something like this: <a href="showtable.php?table=clients&col=lastname"><b>Mysql Table</b></a> This is a general mysql table viewer for a database. showtable.php...
2
by: hera | last post by:
I am new at php and MySQL and have some (ugly, I'm sure) code to populate form select fields with data from a MySQL database, so a user can use those to sort/filter and view the 3,000 records. No...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Ike | last post by:
Recently, I began using a different MySQL verver (i.e. different machine as well as different version#, going from 4.12a to 4.1.9 max). The following query used to work: select firstname,...
1
by: bcochofel | last post by:
Hi, I'm using Perl CGI and HTML::Template to generate the following XML: ---------------------------------------------------------------------- <?xml version="1.0" encoding="iso-8859-1"?>...
6
by: bill | last post by:
I am about to start on a module that will accept a location from a user, use Google geolocation services to get the lat/lon and then compute the distance from the site visitor to about 100 kennels...
1
by: jenson | last post by:
<?php /* * Created on Apr 13, 2007 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ require_once 'init.php'; ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.