MySQL: Sorting by title problem

Discussion in 'MySQL' started by dramalover, Feb 16, 2008.

  1. #1
    Hello, I am pretty new to mysql and need some help with sorting.

    I have a few titles similar to this:

    title part 1
    title part 2
    title part 3 & 4
    title part 5
    title part 6
    title part 7 & 8

    These title are added in mysql in no particular order so I cannot order them by date or id or just by the title.

    I have tried using this:

    select * from titles order by length(title), title ASC

    However it doesn't work as the title with 2 parts messes it.

    Does anyone know how I can order those in a ASC order?

    Thank you.
     
    dramalover, Feb 16, 2008 IP
  2. marksailes

    marksailes Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i would create a new column on that database, call it order make it as an int

    they given each row a value , 1,2,3,4,5 etc

    and order by that, you can then create and change the order to however you want it
     
    marksailes, Feb 17, 2008 IP
  3. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You want to sort titles alphabetically? In that case, why don't you use 'ORDER BY title' ?
     
    CreativeClans, Feb 19, 2008 IP
  4. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ORDER BY TITLE DESC

    ORDER BY TITLE ASC
     
    LittleJonSupportSite, Feb 19, 2008 IP
  5. ayahhelmy

    ayahhelmy Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if there is "title part 10" then it would be overlapped to "title part 2". this could be happen too.

    I agree with marksailes about adding new int field / column so you can fill it with any number you want and use it to sort. If you ever use wordpress, when you create a page (not post) there will be an "order" field. After published, the order of pages will be sorted by that field.
     
    ayahhelmy, Feb 19, 2008 IP
  6. Petey

    Petey Peon

    Messages:
    68
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi dramalover,

    MySQL doesn't include a natural sort option :( but this SQL should work for you :)

    select * from titles ORDER BY substring_index(title, ' ', 1), substring_index(title, ' ', -1)+0;

    HTH
     
    Petey, Feb 20, 2008 IP