Wednesday, November 9, 2011

Mysql - How to search for exact word match using REGEXP?

If you are looking to match EXACT words then don't use LIKE keyword.
You could do word boundaries with the following REGEXP. 
SELECT * FROM TABLE WHERE field_name RLIKE "[[:<:]]foo[[:>:]]";
In Ruby code you can use it like
Model.find(:all, :conditions => "field_name RLIKE 'foo.*'")
or
Model.find(:all, :conditions => "field_name REGEXP 'foo.*'")

No comments:

Post a Comment