Kivo
Kivo

Reputation: 435

SQL Return datas from the second table based on the data of the first table

I have two SQL Server tables that are not linked together (No join) and I want to get the data from the second table based on the data of the first table. In the first table I have this:

Table 1

id  name
----------
4   BOX-A 
8   PART-D

Table 2

id  name
------------
14  BOX-A1
25  BOX-A2
38  TOOL-A1
39  TOOL-A2
40  PART-D1
41  PART-D2

What I want to do is that for each name found in table 1, I want to return all the matches in the table 2, so at the end I will have something like this:

id  name
-----------
14  BOX-A1
25  BOX-A2
40  PART-D1
41  PART-D2

Upvotes: 0

Views: 54

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271051

You can use join or exists:

select t2.*
from table2 t2
where exists (select 1 from table1 t1 where t2.name like concat(t1.name, '%'));

Upvotes: 2

Related Questions