pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: adt09@dial.pipex.com
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Relating 1 table to another.
Date: Sun, 25 Apr 1999 12:47:49 -0400
Message-ID: <10858.925058869@sss.pgh.pa.us> (raw)
In-Reply-To: <1700710081999.925053007172.497941124.adt09@dial.pipex.com.MailpuccinoKongEuTak>

"Nigel Tamplin" <adt09@dial.pipex.com> writes:
> This is ok, but to be more useful I want to show the names of the people
> so I do a
> select project.id, project.name, people.name from project,people where
> project.mainpersonid = people.id;
> That works but only shows the name of the mainperson, ok I can change it
> to show the name of the standby person instead which brings me on to my
> question...
> How can I show the names of both the main person and standby person for
> each project.

This is where you need table aliases (AS clauses).  You do it like this:

select project.id, project.name, p1.name, p2.name
from project, people as p1, people as p2 where
project.mainpersonid = p1.id and project.standbypersonid = p2.id;

p1 and p2 are now independent sources of tuples --- the fact that
they're both scanning the same table is no problem.

What you'd probably really write is

select project.id, project.name,
       p1.name as mainperson, p2.name as standbyperson
etc...

so that you get helpful column titles instead of just "name" twice.

			regards, tom lane



view thread (3+ messages)  latest in thread

Message-ID: <10858.925058869@sss.pgh.pa.us>
Permalink:  ../10858.925058869@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10858.925058869@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, adt09@dial.pipex.com
  Subject: Re: [SQL] Relating 1 table to another.
  In-Reply-To: <10858.925058869@sss.pgh.pa.us>
  References: <1700710081999.925053007172.497941124.adt09@dial.pipex.com.MailpuccinoKongEuTak> <10858.925058869@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox