0

Hello,

I've tried to search solution for selecting name from table1, for table2 id's.
Table structures are like

table1 (persons):
id
name
created

table2 (linking):
sup_id
emp_id

where sup_id is id from table1, and emp_id is id from table1.

Can I directly replace those ID:s with correct person names from table1?

Edited by JesuZ: n/a

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by urtrivedi
0

i think you want to join tables.

query for joining two table is as follows

select t2.sup_id, t1s.name sup_name, t2.emp_id, tle.emp_name 
from table2 t2 
left outer join table1 t1s on t2.sup_id=t1s.id
left outer join table1 t1e on t2.emp_id=t1e.id

Edited by urtrivedi: n/a

0

Thanks, awesome! Exactly what I was looking for!
How about if I want also select records from table1, that doesn't have link in table2?

0

this query returns those id, name from table1 which id are not referred either in sup_id or in emp_id

select id, name from table1 t1
left outer join table2 t2s on t2s.sup_id=t1.id
left outer join table2 t2e on t2e.emp_id=t1.id
where ifnull( t2s.sup_id,0)+ ifnull(t2e.emp_id,0)=0
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.