moteutsch
moteutsch

Reputation: 3831

MySQL: JOIN two tables on LIKE

I have a table with a column containing text, and I want to select all of the tables where the text from one of the rows in a second table appears in that row. Here is a pseudo-query:

SELECT title FROM recipes AS r 
   JOIN ingredients AS i 
   ON r.ingredients LIKE '%' + i.name + '%';

The above query does not work, however. How do I do this correctly?

Upvotes: 2

Views: 5533

Answers (2)

Tarka
Tarka

Reputation: 4043

SELECT title
FROM recipes r JOIN ingredients i ON r.ingredients LIKE concat('%', i.name, '%')

MySQL is weird, and makes you use the concat operator to concatenate strings together. Most others use ||

Upvotes: 3

Ken Bloom
Ken Bloom

Reputation: 58770

You can't concatenate strings with the + operator (it's for arithmetic only). Use concat('%',i.name,'%') instead.

Upvotes: 2

Related Questions