Re: two foreign keys

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Brian <Brrrian(at)Excite(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: two foreign keys
Date: 2002-02-16 22:16:19
Message-ID: web-695980@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Brian,

> I have a table that has two foreign keys to the same table.
>
> - operator_id_start
> - operator_id_stop
>
> Both point to the operator table.
>
> When displaying the row from the table how do I join so that I can
> have both Operator table details available?

Aha! I love the classic novice questions. So much easier than "I
can't get Perl::SSElay to connect with Pgsql>"

The bit of SQL knowledge you're missing is called "table aliasing."
This is where you give a table a different name in the query than its
real name. Aliasing allows you to refer to the same table more than
once in a query.

So, to answer your question, making up quite a bit of detail about your
tables that I don't have:

SELECT session_id, session_name, op_start.operator_name,
op_end.operator_name
FROM sessions
JOIN operators op_start ON sessions.operator_start_id =
op_start.operator_id
JOIN operators op_end ON sessions.operator_end_id =
op_start.operator_id;

Got it? Now go buy yourself a copy of "SQL Queries for Mere Mortals."

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2002-02-16 23:26:00 Re: two foreign keys
Previous Message Patrick Hatcher 2002-02-16 21:58:33 Re: Updated to 7.2 now can't createlang..