Henrique
Henrique

Reputation: 529

Join two tables field based on the same id?

Need a help joining two tables based on the same id.

Table 1:

id_1 | id_2

Table 2:

id | name

I need a query where looks for id_1 on table 2 field id and return the field name and also looks for id_2 on table 2 field id and return the field name.

An example would be:

Table 1:

1 | 2

Table 2:

1 | Joe
2 | Michael

Return would be:

Joe | Michael

Thanks,

Upvotes: 3

Views: 4089

Answers (1)

Adriano Carneiro
Adriano Carneiro

Reputation: 58615

This is what you are looking for:

select t21.name, t22.name
from Table1 t1 
inner join Table2 t21 on t1.id_1 = t21.id
inner join Table2 t22 on t1.id_2 = t22.id

Upvotes: 8

Related Questions