Re: Two joins on same foreign key

From: Anony Mous <a(dot)mous(at)shaw(dot)ca>
To: 'Michael Glaesemann' <grzm(at)myrealbox(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Two joins on same foreign key
Date: 2004-01-31 06:35:03
Message-ID: 006601c3e7c4$5c0da430$7402a8c0@PETER
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Actually, given that there may not always be a backup_employee field (can be
null) I think I'm forced to use an outer join to return all team leader
records regardless if a matching backup_employee record exists. I'll test
yours out and see.

Thank you!

-----Original Message-----
From: Michael Glaesemann [mailto:grzm(at)myrealbox(dot)com]
Sent: January 30, 2004 11:21 PM
To: Anony Mous
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] Two joins on same foreign key

On Jan 31, 2004, at 7:03 AM, Anony Mous wrote:
> 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?  Any help is greatly appreciated!

I think you could handle this by calling table1 twice, but with
different aliases, like so

SELECT leader.employee_name, backup.employee_name
FROM table1 leader, table1 backup, table2 t2
WHERE
leader.employee_id = t2.teamleader_employee_id AND
backup.employee_id = t2.backup_employee_id

Does that work for you?

Michael Glaesemann
grzm myrealbox com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2004-01-31 07:17:26 Re: Creating 'global' functions.
Previous Message Michael Glaesemann 2004-01-31 06:20:33 Re: Two joins on same foreign key