views:

29

answers:

1

Hey,

I know most of the time people just do a

LIKE '%$search_query%'

but see the thing is, in my case someone may be searching for things like

that cow is fat

now, the thing is in my database i wouldnt be storing it exactly as "the cow is fat" i'd be storing it something like "fat cow" so using

LIKE '%$search_query%'

wouldn't work.

Can anyone point me in the right direction for an efficient way to search my database like I have explained?

Thanks

+2  A: 

You are looking for full-text search, with the restriction that the default minimum word length there is 4 by default, so it won't work for your examples.

An example that searches for "house" and "barn" in records that do not contain "sheep" in boolean mode:

SELECT * FROM table WHERE MATCH (columnname)
      AGAINST ('+house +barn -sheep' IN BOOLEAN MODE);

Alternatively, natural language may work better for full sentences.

Full text search is a rather complex field, though, with many caveats and gotchas. It is worth reading the manual carefully.

Pekka
alright, I got it working but now for some odd reason I'm having 1 issue. It's not returning any results when I search for "Like" (when it should be) but it returns them fine for everything else. Could it be ignoring it because it's commonly used in mysql query strings?
Belgin Fish
@Belgin that's why I you need to read the manual for the gotchas :) See [mySQL Stopwords](http://dev.mysql.com/doc/refman/5.1/en/fulltext-stopwords.html)
Pekka