开发者

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 whe开发者_开发知识库re column regex '/\b".$text."\b/i'"

but that didn't work.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜