MySQL: What is a reverse version of LIKE?

LIKE operator in MySql is used to find rows that contain our query text, for example:

select name from user where name like "%john%"

which will return John Smith, Peter Johnson etc.

What if I need the opposite – to find rows that are CONTAINED in our query text? For example I give it John Smith and Peter Johnson are best friends and want it to find me all names from the table that could be found in this string.

How to do that?


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, '%')