Need Help To Fetch Data In Alphabetic

Discussion in 'MySQL' started by salayhin, Nov 11, 2009.

  1. #1
    I have a music site. Where song will be categorized like A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z and all letters have a active link.

    I need to Fetch Data In Alphabetic.

    That means when i press "A" then all data with first name "A" or "a" will be shown.

    How can i do it?

    Please help me.
     
    salayhin, Nov 11, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    it can be done using query similar to following..

    SELECT * FROM table_name WHERE song_name LIKE 'A%'

    If you can post table structure and examples, we can help you get exact query.
     
    mastermunj, Nov 11, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I would do:

    SELECT * FROM table_name WHERE UCASE(song_name) = 'A';

    LIKE statements tend to be hard on resources.
     
    jestep, Nov 12, 2009 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    @jestep, unless there is a separate field storing first letter of song name, the query you gave won't yield desired result.

    Like are little hard but with proper index they can give fast results.

    I would suggest to keep a separate column having first letter of song name in it and use the query given by jestep on that column to retrieve the data :)
     
    mastermunj, Nov 12, 2009 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    mastermunj's first query will return the desired results.

    This query requires full table scan even if you create an index on this column due to row operation (ths use of UCASE).


    No need to create a separate column to keep the first letter of song name. Instead create an INDEX on first 1, 2 or more characters (according to your requirement) of the song name and use the query provided by mastermunj. This kind of LIKE query uses index.

    Like queries containing condition similar to '%A' are resouce intensive and requires full table scan.
     
    mwasif, Nov 13, 2009 IP