1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Searching Mysql Database for Alphabet Listing?

Discussion in 'MySQL' started by goldensea80, Aug 22, 2005.

  1. #1
    Yes, I dealing with this. Do you have idea? Links to articles?...
    Thanks a lot!
     
    goldensea80, Aug 22, 2005 IP
  2. vectorgraphx

    vectorgraphx Guest

    Messages:
    545
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Might be a bit more specific... what exactly are you trying to do?

    select a group of records where field value begins with "A" or "B" etc... depending on user input?

    if that's the case, use the % wildcard in your where clause.

    sql = "SELECT * from yourtable WHERE yourfield LIKE '" & user_input_variable &"%'"

    (notice, the wildcard is after the user input variable, not before. if you want to match any part of the field, put the wildcard both before and after the user variable.)

    If you're just wanting to sort alpha, that part is much easier. Just use the ORDER clause with either asc (ascending) or desc (descending). if it is an alpha field, it will assume the ascending order is alpha.

    sql = "SELECT * from yourtable ORDER by yourfield ASC"

    Otherwise, we'll need more info if these examples don't fit your needs.


    VG
     
    vectorgraphx, Aug 22, 2005 IP
  3. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Thanks,
    Here is what I did and it work nicely so far:
    
    $where="song_name REGEXP '^[$keywords]'";
    $sql="SELECT * FROM tb_songs
           WHERE $where
           ";
    
    PHP:
    To check if a song associate with a keywords exists
    function is_song_abc($keywords)
    {
         global $_CONNECTION;
         $where="song_name REGEXP '^[$keywords]'";
         $sql ='SELECT count(*) as total
                FROM '.TB_SONGS."
                WHERE $where";
         $result = sql_query($sql);
         return mysql_result($result,'total');
    }
    PHP:
    See the attachment!
     

    Attached Files:

    • abc.JPG
      abc.JPG
      File size:
      6.4 KB
      Views:
      2,613
    goldensea80, Aug 22, 2005 IP
  4. av1

    av1 Active Member

    Messages:
    168
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4
    try this: select * from db where name like ('a%');
    takes only those records that start with letter a
     
    av1, Aug 27, 2005 IP
  5. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #5
    That's right.
    But in case of the records that start with numbers (0-9), that won't works. But the method with REGEXP works. You can check my block for more detail
     
    goldensea80, Aug 28, 2005 IP
  6. av1

    av1 Active Member

    Messages:
    168
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #6
    hmm, for numbers i use select * from db where name like '0%' or name like '1%' or... u get the idea.
    i wonder which is faster, ur regexp thing or my 10 or's
     
    av1, Aug 28, 2005 IP
  7. Gmorkster

    Gmorkster Peon

    Messages:
    202
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    REGEXP '^[a-z0-9]' ?
     
    Gmorkster, Aug 28, 2005 IP
  8. av1

    av1 Active Member

    Messages:
    168
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #8
    dude, u totally missed the point, the guy wants to get a list of records starting with numbers, so it would be '^[0-9]'
     
    av1, Aug 28, 2005 IP
  9. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #9
    That's right. I don't know what will be faster (Maybe I'll test it infuture). But the REGEXP code will be shorter for both number and character. You don't have to change the search code, and the keywords array will be: '0-9','a',...,'z'
     
    goldensea80, Aug 28, 2005 IP
  10. Gmorkster

    Gmorkster Peon

    Messages:
    202
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Guess I did miss the point, heh

    Run a test with ab (it's in the Apache bin dir by default) to see which one's faster
     
    Gmorkster, Aug 28, 2005 IP
  11. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #11
    * For REGEXP '^a'
    For LIKE 'a%'
    So that "LIKE" method is faster.

    * For Fields started with Numbers
    song_name REGEXP '^[0-9]'
    song_name LIKE '0%' OR song_name LIKE '1%' OR song_name LIKE '2%' OR song_name LIKE '3%' OR song_name LIKE '4%' OR song_name LIKE '5%' OR song_name LIKE '6%' OR song_name LIKE '7%' OR song_name LIKE '8%' OR song_name LIKE '9%'
    So that "REGEXP" method is faster.
     
    goldensea80, Aug 29, 2005 IP
  12. av1

    av1 Active Member

    Messages:
    168
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #12
    thnx for the numbers :)
     
    av1, Aug 29, 2005 IP
  13. Gmorkster

    Gmorkster Peon

    Messages:
    202
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    and this is how it should be, the REGEXP involves one (resource consuming) operation while the 10 LIKE's are 10 operations logically ORed
     
    Gmorkster, Aug 29, 2005 IP