Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select two columns to join with one column 1

Status
Not open for further replies.

stevetektip

Programmer
Joined
Oct 17, 2002
Messages
6
Location
US
I've got a table with two id columns which are foreign keys into the same table:

table1
========
repID
asstRepID

table2 (names of reps)
======================
id
name

I want to retrieve a row that has the name of both the rep and asst rep, such as:

repID repName asstRepID asstRepName
---------------------------------------------
234 D. Duck 381 M. Mouse

I've tried joining as follows:

select repID, name as repName, asstRepID from table1 join table2 on table1.repID=table2.ID

which gives me the repName, but I'm getting syntax errors for anything I've tried to get the asstRepName with joins, sub queries, etc.

Any guidance is greatly appreciated.
Thanks!
 
Try this

select c.repid,c.repname,c.asstrepid,name as asstRepName from table2
inner join
(select repID, name as repName, asstRepID from table1 join table2 on table1.repID=table2.ID)as C
on C.asstrepid =table2.id
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top