简体   繁体   中英

MySQL REGEXP search multiple words

The field Products.Name contains PETISCO NESTLE PURINA DOG CHOW CARINHOS MIX DE FRUTAS

I need search de word DOG and the word NESTEL (wrong word) but the first three letters are correct.
I don't know the order of the words.

I tried same thing like this REGEXP '[^DOG][^NESTEL]{3}' but doesn't work!

thanks

Update 1:

When the user types DOG and NESTEL I want my app to show all products that contain the words DOG and NESTLE, even part of the word is wrong!

Update 2:

where Products.Name REGEXP '[[:<:]]?=*NES.*[[:>:]]' and Products.Name REGEXP '[[:<:]]?=*DOG.*[[:>:]]'

This shows me the dog E NES but if the user types NESTEL and not NESTLE?

You need the levenshtein function for mysql. It goes through all the results evaluating them according to it's similarity to the searched word (ie NESTEL) and return them with that extra ratio field. Then you only have to select the one with a higher ratio, or a list with the ones more similar... etc.

SELECT ...
    FROM ...
    WHERE Name REGEXP '[[:<:]]dog[[:>:]]'
      AND Name REGEXP '[[:<:]]nes';
  • You wanted "dog" to be a "word", hence the word boundary thingies.
  • You wanted some "word" starting with "nes", hence the word-start thingie only.
  • It does not check for 6 letters in "nes...", that would be

      AND Name REGEXP '[[:<:]]nes[az]{3}[[:>:]]'; 

There may be a collation issue; please provide SHOW CREATE TABLE .

Mini-lesson:

  • [^DOG] says "match 1 bytes, but not D , nor O , nor G .
  • [^NESTEL]{3} says match exactly 3 bytes, but they cannot be any of those 6 letters.
  • MySQL does not handle any of the operators _starting with ?
  • NES.*[[:>:]] matches NES , followed by the rest of the string -- This is because * is "greedy" and that [[:>:]] will match the end of string.
  • LIKE '%dog%nes%' has no tests for word boundries.
  • nes*** as a regexp would say ne followed by any number of s , including zero.
  • nes... would match nes plus 3 bytes of any kind. So, all it says it that the nes must not be too near the end of the string.
WHERE some_col LIKE '%dog%nes%' or some_col LIKE '%nes%dog%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM