Hello!
I have a table with a client name column, and in a form, I need to select a client by supplying his full name, or if s/he doesn't exist, showing clients with similar names so that the user can select from a list.
That I can resolve with a simple WHERE name="$name", the 'name' column being indexed.
But here comes the problem. When such select yields 0 entries, I want to find SIMILAR names, matching as much of characters from the left as possible. For example, if I'm looking for "John Doe", I want the script, failing to find him, to show me similars like:
Johan Doe
Johen Doe
John Doae
...
I don't know if MySQL has a string comparison function that would do this, so I decided to make an algorithm:
If WHERE name=$name fails, then I'd do multiple selects, in a loop, like this:
1st iteration: name LIKE "J%"
2nd iteration: name LIKE "Jo%"
3rd: name LIKE "Joh%"
...
until an iteration finds 0 results, for example at "John%"
Then, I'd go one step back ("Joh%") and select all results where name LIKE "Joh%". Of course, instead of retrieving entire sets of rows matching the query, I'd only COUNT(*) them and stop when count is 0 and then go one step back (by remembering last matching string), and then I'd select full set of rows.
I hope I explained this well. Any comments, or easier solutions? Thanks in advance.