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

table1 (persons):

table2 (linking):

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?

Recommended Answers

All 3 Replies

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

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?

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.