kingsley
kingsley

Reputation: 305

match a word from a sentence in MYSQL using regex

How can I match a word in a sentence from a column using regex? I tried:

"select * from table where column regex '/\b".$text."\b/i'"

but that didn't work.

Upvotes: 2

Views: 3072

Answers (3)

RobertPitt
RobertPitt

Reputation: 57268

I think you want the LIKE clause!

SELECT * FROM table WHERE column LIKE '%value%'

Also please read UltimateBrent's answer as full text search is a very good point.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838106

Try this:

"SELECT * FROM table WHERE column REGEXP '[[:<:]]" . $text . "[[:>:]]'"

You should make sure that any characters that could be interpreted as special characters in $text are properly escaped. You should also ensure that you do not get an SQL injection.

Upvotes: 3

Brent
Brent

Reputation: 23702

MySQL is particularly bad about stuff like this. I'd recommend using MATCH AGAINST syntax http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html or using something like SOLR if you're going to be doing this in volume.

Upvotes: 3

Related Questions