简体   繁体   中英

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM