Let us say I have a table users table with a column name and it has the following records:
- charlie
- sheen
- charl
- een
- arnold
Can I get the records that will match with “charlie sheen” without looping each item?
To be more specific: Is there a way to fetch records that contain fields that are substrings of a given string?
Well, you can do what you want:
See the SQLFiddle example.
Keep in mind that this will be inefficient, though: by the very nature of the problem, you will have to treat every single row as a regex to see if it matches.
There will also be problems if the
namecolumn could contain characters that have a special meaning within a regex.Edit: also worth mentioning is that if you allow non-trusted users to enter names in the database (such as from a website), treating it as a regex is a potential security problem.