473,394 Members | 1,154 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,394 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 2468
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.