LIKE or REGEXP

Discussion in 'Databases' started by bumbar, Jul 11, 2014.

  1. #1
    Hello,

    I have table with phone number... I have to do a search query number

    Simple
    id number
    1 123456
    2 223344
    3 9999

    if I test with query:
    "SELECT*
    FROM`phones`
    WHERE number LIKE '%223344%'"

    find numbers - succeess!

    But if I select like this:
    "SELECT*
    FROM`phones`
    WHERE number LIKE '%059223344%'"

    Or

    "SELECT*
    FROM`phones`
    WHERE number LIKE '%223344111%'"

    Not found....

    How do I find the number to the query? Perhaps REGEXP?

    Thank you!
     
    bumbar, Jul 11, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    If the number is not in the database (as the last two aren't), of course you won't find them.
     
    PoPSiCLe, Jul 11, 2014 IP
  3. spujap

    spujap Active Member

    Messages:
    136
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #3
    For number LIKE '%223344111%'" do you want to find whether any combination of digits such as 223, 22334, 223344 is present in the table?
     
    spujap, Aug 16, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    The question reveals low understanding for the mysql query. LIKE doesn't test to see if anything provided between the %signs are in the queried dataset, it tests to see if anything in the dataset contains the provided content - hence, if you want to search for any phone number containing 223 you search for that - you don't search for 223111. You limit the search with the minimal amount of input needed to find the results you need.
     
    PoPSiCLe, Aug 19, 2014 IP