tags:

views:

194

answers:

10

Hi Friends,

I want to search like this: the user inputs e.g. "murrays", and the search result will show both records containing "murrays" and records containing "murray's". What should I do in my query.pl?

+2  A: 

I would first build a search column which has the text without punctuation and then search on that. Otherwise you'll have have to have a series of regular expressions to search against or check individual records in PHP for matching: both of which are computational intensive operations.

Richy C.
A: 

If this is for single word searching, you could try using Soundex or Metaphone functions? These would handle sounds-like as well as spelling

Not sure if MySQL has these, but PHP does (which would require separate columns to hold these values).

Otherwise, Richy's no-punctuation extra column seems best.

Peter Boughton
+1  A: 

Maybe something like this: (untested!)

SELECT * FROM users WHERE REPLACE(user_name, '\'', '') = "murrays"
Max
A: 

You could try adding a replace to your query like this

replace(name, '''','')

to temporarily get rid of the apostrophes for the match.

select name from nametable where name = replace(name,'''','');
Neosionnach
That wont work backwards.
Peter Boughton
+3  A: 

This won't help if you absolutely need to do these queries in SQL, but if you can set up a Lucene search index for it, you gain a lot of this kind of "fuzzy search" functionality. Note though that Lucene is quite a complex topic by itself.

deceze
Good work on the question title :)
Roee Adler
Draws a lot more attention, doesn't it? Good job deciphering the original question. :D
deceze
+3  A: 

What do you think about using the SOUNDEX function and the SOUNDS LIKE operator ?

That way, you can simply do:

SELECT * from USERS WHERE name SOUNDS LIKE 'murrays'

I'm pretty sure it doesn't work for every case, and perhaps it is not the most efficient way to solve the problem, but it could fit your needs.

Guido
It's definitely an option, but it will come with a computational penalty. It'll really depend on exactly what the original poster is asking to do (a single test case doesn't help us pick the "best" solution).
Richy C.
+3  A: 

What you could do is create an extra field in the database, which contains the data with all special characters stripped from it, and search there. A bit lame, I know. Looking forward to see smarter answers ;)

x3ro
+1 for an indexable solutions - all replace(column, '\'', '') will ignore all indexes
ck
+4  A: 

Quick and dirty:

SELECT * FROM myTable WHERE REPLACE(name, '\'', '') = 'murrays'
Jack Ryan
Nice one. .........
Peter Perháč
This is pretty much the easiest/quickest way!
James
Assuming of course you are pre-processing the search string and removing the apostrophe beforehand...
James
It'll work, yes, but on a big data set you'll hit a bit speed penalty as you are having to pre-parse every "name" before comparison.
Richy C.
Thats what i meant by quick and dirty. But any solution is going to have this problem at some point. You have to remove the character to do the comparison. It's just a question of when, and how, you do it.
Jack Ryan
A: 

This query should be able to pick up "murrays" or "murray's".

var inputStr = "murrays";
inputStr = String.Replace("'", "\'", inputStr);
SELECT * FROM ATable WHERE Replace(AField, '\'', '') = inputStr OR AField = inputStr
James
A: 
  1. strip user input and names in database from all non-letter characters.
  2. Use levenstein distance or soundex to find murrays with murray or marrays. This is optional but your users would love that.
vava