I have a form with an input box where user can specify their names, names can be two or more words eg John Smith or John Michael Smith.
I need to write a query which would return the records including ALL words in the submitted name. So query will return records which has all those name words but can have different order.
For example, if search string is John Michael Smith, query should be able to return records with names such as John Smith Michael, Michael Smith John, Smith John Michael or other combination with all those words there. As can be seen return only records which still has all words in name field but can have different order. It should not however return results which do not contain a name part for example John Michael should not be returned since it is missing Smith.
I can’t figure out how to write a query for such requirement that I have. Please help.
Update
The answers provided so far return even the records that match John Michael also. I tried answers of @Marco, @abesto and @Eddie.
The problem still persists 🙁
You could try a fulltext search:
In this solution, ‘+part1 +part2 +part3’ would be generated in PHP.
See the MySQL docs