Sorting by first character (PHP & Mysql)

Discussion in 'Programming' started by Cypherus, Sep 7, 2007.

  1. #1
    Mods: Since im not sure if this is done in MySQL or PHP, I just placed it in the general programming category. Feel free to move it If its wrong.

    Hi there,

    I have a mysql database containing alot of destinations. When I say alot, i mean thousands, so i can't list them all up on one single page, so I want to sort them alphabetically. On top of the page, I want a list from A to Z. When someone presses A, all destinations starting with A is printed. When someone press B, all destinations starting with B listes, and so on. What would be the best way to do this?

    Please, feel free to give some dummy examples.
     
    Cypherus, Sep 7, 2007 IP
  2. amf-flt

    amf-flt Active Member

    Messages:
    100
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #2
    ... WHERE SUBSTR(name, 1, 1) = 'A' ORDER BY name

    something like that. the manual is a better place to look than my posting :)
     
    amf-flt, Sep 7, 2007 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    This is how I do it.

    if letter is A:

    $letter = 'A';
    WHERE `title` LIKE '$letter%'

    or if it is not searchable use < > tags with

    $next_letter = $letter++;

    Peace,
     
    Barti1987, Sep 7, 2007 IP
  4. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That'll do it, or you can use LEFT. Add in LOWER for case insensitivity:

    
    select * from `your_table` where LOWER(LEFT(`your_column`,1)) = 'a'
    
    Code (markup):
     
    sea otter, Sep 7, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    If I'm not mistaken, MySQL searches case-insensitively by default anyway.
     
    nico_swd, Sep 8, 2007 IP
  6. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I wasn't certain. I know that ORDER BY is insensitive, but I wasn't certain about the main select clause. And being Saturday, I'm too lazy to look it up :p
     
    sea otter, Sep 8, 2007 IP
  7. Sharky01276

    Sharky01276 Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Depends on the field. I believe if its defined as text varchar then its fine. If its a blob then its case sensitive. Makes sense really..
     
    Sharky01276, Sep 8, 2007 IP
  8. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yup, you're right about varchar. Just confirmed with a couple of queries.

    So the code I posted can be simplified to

    
    select * from `your_table` where LEFT(`your_column`,1)='a'
    
    Code (markup):
     
    sea otter, Sep 8, 2007 IP
  9. Grumps

    Grumps Peon

    Messages:
    592
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    
    // this will return all the names that starts with A
    SELECT * FROM yourtable WHERE thecolumnname LIKE "A%"
    
    // this will return all the names that starts with B
    SELECT * FROM yourtable WHERE thecolumnname LIKE "B%"
    
    PHP:
     
    Grumps, Sep 9, 2007 IP