Mysql – Using a subquery in a LIKE statement

likeMySQLsubquery

I need to write a subquery inside a LIKE statement. This subquery would return me a word that I need to find in the middle of a sentence (that belongs to another field). Is it possible? This is how I'm trying (and failing):

select nm_linha from linhas where cidades_origem like '%SUBQUERY HERE WOULD GO HERE%'

Best Answer

Something like this should work :

select nm_linha from linhas where cidades_origem like CONCAT('%', (subquery_goes_here) ,'%')

For example :

SELECT * 
FROM films
WHERE titre LIKE CONCAT('%', (SELECT  'fella'),  '%')

+---------+------------+
| id      | titre      |
+---------+------------+
|       1 | Goodfellas |
+---------+------------+
1 row in set (0.01 sec)