Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 758
Henrique Avatar asked Mar 22 '26 21:03

Henrique


1 Answers

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
like image 188
Adriano Carneiro Avatar answered Mar 25 '26 09:03

Adriano Carneiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!