Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 317
Mossawi Avatar asked Apr 09 '26 01:04

Mossawi


2 Answers

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.

like image 99
Paul Avatar answered Apr 11 '26 15:04

Paul


Can you search for a space as well?

SELECT * 
FROM table 
WHERE text LIKE '% ell%'
OR text LIKE 'ell%' // needed if string starts with ell
like image 39
Matt Busche Avatar answered Apr 11 '26 14:04

Matt Busche



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!