SQL Join on a column LIKE another column

Don P picture Don P · Feb 4, 2013 · Viewed 69.6k times · Source

Possible Duplicate:
mysql join query using like?

I want to do a join where one column contains a string from another table's column:

SELECT
a.first_name,
b.age
FROM names a
JOIN ages b
ON b.full_name LIKE '%a.first_name%'

Is this possible? I'm using MySQL. Of course the above query will not work since the LIKE '%a.first_name%' will just look for the string a.first_name, and not the column's actual value.

Answer

colin-higgins picture colin-higgins · Feb 4, 2013

You only need to concatenate the strings, you could also do a search and replace.

SELECT
    a.first_name,
    b.age
FROM names a
JOIN ages b
ON b.full_name LIKE '%' + a.first_name + '%'