Find Exact Word from String in mysql

Find Exact Word from String in mysql

In this blog, you will learn how to find exact words from strings in a mysql database, sometimes we need to match an exact word from a string. In mysql developers mostly use like clause to match words but it does not match an exact word, so it gives the wrong output.

We will use regex, a regex called regular expression to match an exact word from a string. It will return the exact word, it is better than like clause it will give the correct output.

Example:

144,14,44,441,143,1454

Suppose you have stored such type of data in mysql column and you want to find out 14 from this string.


Solution:

SELECT *
FROM `your-tablename`
WHERE your-column-name REGEXP '[[:<:]]".14."[[:>:]]'


In the above query replace tablename with your tablename, replace your-column-name with your column name and between REGEXP we have written 14 , because we want to match 14 string. You can put the string which you want to match.