Re: two foreign keys - Mailing list pgsql-novice

From Josh Berkus
Subject Re: two foreign keys
Date
Msg-id web-695980@davinci.ethosmedia.com
Whole thread Raw
In response to two foreign keys  (Brian <Brrrian@Excite.com>)
Responses Re: two foreign keys
List 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@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco

pgsql-novice by date:

Previous
From: Brian
Date:
Subject: two foreign keys
Next
From: "Patrick Hatcher"
Date:
Subject: Re: Updated to 7.2 now can't createlang..