471,854 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,854 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 2371
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Roger Lord | last post: by
reply views Thread by Philip Stoev | last post: by
39 posts views Thread by Mairhtin O'Feannag | last post: by
1 post views Thread by Ike | last post: by
1 post views Thread by bcochofel | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

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.