Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Join on a column LIKE another column [duplicate]

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.

like image 304
Don P Avatar asked Feb 04 '13 22:02

Don P


People also ask

How do I use like between two columns in SQL?

The SQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

How can we avoid duplicate records in SQL while joining two tables?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.

Why join query returns duplicate rows?

Using an Incomplete ON Condition. Unwanted rows in the result set may come from incomplete ON conditions. In some cases, you need to join tables by multiple columns. In these situations, if you use only one pair of columns, it results in duplicate rows.


1 Answers

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 + '%' 
like image 51
colin-higgins Avatar answered Oct 13 '22 00:10

colin-higgins