MySQL sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ruby Tuesday

    #1

    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



  • André Næss

    #2
    Re: MySQL sort

    Ruby Tuesday:
    [color=blue]
    > 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[/color]

    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

    Comment

    • David Mackenzie

      #3
      Re: MySQL sort

      On Mon, 16 Feb 2004 01:45:53 -0500, "Ruby Tuesday"
      <rubytuezdayz@y ahoo.com> wrote:
      [color=blue]
      >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[/color]

      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 )

      Comment

      Working...