How to combine all the word in SELECT FROM WHERE like?

Discussion in 'PHP' started by youlichika, Oct 14, 2010.

  1. #1
    $result=mysql_query("SELECT* FROM name WHERE name like 'name1' OR name like 'name2'...");
    HTML:
    I have more than 20 words add into SELECT FROM WHERE like. Is there anyway to short the code?
    Such as: $result=mysql_query("SELECT* FROM table WHERE name like (name1, name2, name3, name4...) );
    Of course this code is wrong, just an analogy.
     
    youlichika, Oct 14, 2010 IP
  2. TYPELiFE

    TYPELiFE Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    TYPELiFE, Oct 14, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    $result=mysql_query("SELECT* FROM table WHERE name RLIKE '(name1|name2|name3|name4)'");
    PHP:
    RLIKE :)
     
    koko5, Oct 14, 2010 IP
  4. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    HI koko5, I also tried RLIKE, but RLIKE like '%name1%'.
    And I want precise search 'name1'
    Do you have any good idea?
    Thanks.
     
    youlichika, Oct 14, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    You've to use RLIKE with regular expression :
    
    ...RLIKE '(john|doe)';
    
    Code (markup):
    ^^Will match John Smith,Johnny B., Jack Doe and so on.

    For exact match use IN:
    
    SELECT * FROM table WHERE name IN ('John','Johnny');
    
    Code (markup):
    RLIKE is synonym for REGEXP, so you must specify pattern instead of using %
    Hope it's clear now.
    Regards :)
     
    koko5, Oct 14, 2010 IP
  6. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    Thanks koko5, it is a great tutorial. it's clear for me now.
    Best wishes :-]
     
    youlichika, Oct 14, 2010 IP
  7. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #7
    Thanks, youlichika, I'm glad that I helped a little bit :)
     
    koko5, Oct 14, 2010 IP