Search code examples
phpmysqlsqlsql-like

Get exact match with LIKE - SQL/PHP


Given the text:

"Hello, I'm from Hell",
"Hello, I'm from Ell"

and the following SQL clause:

SELECT * FROM table WHERE text LIKE '%ell%'

I get both the texts above, but I don't want to get both texts, because I was looking for the text "Ell" and not "Hell"

If anyone knows what I mean, can you help me out?

Thanks in advance!

EDIT:

BETTER EXAMPLE

Like when you want to look for the word 'big' but it can't be part of any other word like 'bigger' or 'biggest'


Solution

  • You could use the MySQL Regex word boundary matching:

    SELECT * FROM table WHERE text REGEXP '[[:<:]]ell[[:>:]]'
    

    This matches a string which cosntains the word ell with a word boundary on either side.