Hi, doing a search on mysql.
The user entered the search query 'John Smith', you need to get the lines where the query substring is found in various combinations

one)

John smith loren ipsum ...

... Loren ipsum john smith .

... Loren john smith ipsum ....

2)

Loren john ipsum smith amet

Trying to make a request. With paragraph 2, everything is clear:

SELECT * FROM `person` WHERE memo LIKE '% John % Smith %' 

With paragraph 1 no.
Request:

 SELECT * FROM `person` WHERE memo LIKE '%John%Smith%' 

will return us including such lines:

Loren el john athan ipsum le smith el amet

And I do not need it, we need lines, as indicated in paragraph 1.

I understand that you need to use regexp?
Thank.

    3 answers 3

    In your case it is better to use full-text search in MySQL.
    In a nutshell you can not tell, there is a whole article on this issue.
    One time I did a "smart" search, but then I cut off each word by 1-2 characters from the end, so that the search would understand better the declensions of different words.
    Read more here: http://www.mysql.ru/docs/man/Fulltext_Search.html

      Spaces before and after words?

       WHERE memo LIKE '% John Smith %' 
      • Yes, so we cut off the type matches of such lujohn Smithy - pavelchervov

      Option with regular expressions (in case you decide to do with them):

       SELECT * FROM person WHERE memo REGEXP '[[:<:]]John[[:>:]]' AND memo REGEXP '[[:<:]]Smith[[:>:]]'