Match Only Word in MySQL
Sometimes you want to search for a specific word in a MySQL VARCHAR or TEXT field. For example, let’s say you have a field called description in a table called mytable, and you want to search for the word car in the description field. Now if you use this query: SELECT description FROM mytable WHERE description LIKE ‘%car%’; you will get not only the list of descriptions with the word car, but also those with the words care, careless, card, caring, carpenter, etc… If you want only the word car, then you can use the following advanced query (it has regular expressions): SELECT description FROM mytable WHERE description REGEXP ‘[[:<:]](car)[[:>:]]’
The above query will return results with the word car only. Enjoy!
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply