469,315 Members | 1,585 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

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 2262
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 zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.