If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Multiple inner joins on same field or nested query?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-30-04, 21:50
pjme7154 pjme7154 is offline
Registered User
 
Join Date: Sep 2003
Posts: 35
Multiple inner joins on same field or nested query?

Hi,

I'm wondering how I can structure an SQL statement to perform either a multiple join on a single table, or possibly using a sub-query. Basically, I've got one table in which both fields are foreign keys to another table, as follows:

Table #1

employee_id (pk)
employee_name

Table #2
teamleader_employee_id
backup_employee_id


both fields in table 2 need to do a lookup in table 1 to get the name of the actual employee. Do I need to use nested queries to accomplish this? Is it possible to do two inner joins on the same table? (I've tried this, unsuccessfully!) Any help is greatly appreciated!

-PJ
Reply With Quote
  #2 (permalink)  
Old 01-30-04, 22:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
two joins to the same table, using table aliases to distinguish which table the rows are from, and column to distinguish the columns in the result set
Code:
select lead.employee_name as teamleader
     , bkup.employee_name as backup
  from table2
inner
  join table1 as lead
    on teamleader_employee_id = lead.employee_id      
inner
  join table1 as bkup
    on backup_employee_id = bkup.employee_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-30-04, 23:00
pjme7154 pjme7154 is offline
Registered User
 
Join Date: Sep 2003
Posts: 35
Smile Thanks!

Thank you very much. Works like a charm!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On