MySQL: What is a reverse version of LIKE?
select name
from users
where instr('John Smith and Peter Johnson', name) > 0
I would rather use this method instead of:
select *
from users
WHERE 'John Smith and Peter Johnson' LIKE CONCAT('%', name ,'%')
because if there is any chance that the name
could contain the %
or _
character (eg. name='v%alue') then the above query would still return that record incorrectly. Also note that if your column could contain backslashes and/or "C escape characters" then they would also need to be escaped. This is all explained in MySQL String Comparison Functions. For example the below SQL would return 1:
SELECT 'val\%ue' LIKE CONCAT('%', 'al\\\\\%u' ,'%');
The single backslash needed to be escaped with \\\\
and the %
character was escaped: \%
.
Here's a way you can achieve what you describe:
SELECT name FROM user
WHERE 'John Smith and Peter Johnson are best friends' LIKE
CONCAT('%', name, '%')