Complex SELECT, is this possible?

Discussion in 'MySQL' started by mahmood, Oct 30, 2006.

  1. #1
    I try to explain what I need through an example:

    Lets say I want to compare all the strings in my database with a give string. I have to find all the strings that contain at least %70 of characters of my given string. For example if the given string is "abcedfg", I can accept "abedfgm" (6 of 7), but not "dfghigk" (only 3 of 7).

    The way that I know now is to "SELECT" all the fields and then using PHP compare them but the problem is that the database is very huge and this takes too long.

    If I use something like this:
    
    SELECT * FROM myTable WHERE myField LIKE '%a%' OR myField LIKE '%b%' OR myField LIKE '%c%' OR myField LIKE '%e%' OR myField LIKE '%d%' OR myField LIKE '%f%' OR myField LIKE '%g%'
    PHP:
    I would get slightly less results bu still a lot more than I should return because it returns all the strings that have 1 of 7 character in them whereas I want about 5 of 7. If I changed OR to AND, I would only get those which have all the characters.

    I hope I explained it correctly.

    Any Idea guys?
     
    mahmood, Oct 30, 2006 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,832
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #2
    sarahk, Oct 30, 2006 IP
  3. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #3
    mahmood, Oct 30, 2006 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,832
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Indeed, but nothing ventured nothing gained.

    You'll find it's worth the effort.
     
    sarahk, Oct 30, 2006 IP
  5. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I couldn't agree more. :)

    I guess I'd better find someone to do this for me this time.
     
    mahmood, Oct 30, 2006 IP
  6. number7

    number7 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I suppose this will help:

    SELECT * FROM myTable WHERE myField REGEXP '[a-g]';

    Happy Coding ! :)
     
    number7, Nov 3, 2006 IP