pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: Slawek Jarosz <Jarosz.S@ems-t.ca>
Cc: pgsql-sql@postgresql.org
Subject: Re: Optional join
Date: Thu, 14 Aug 2003 23:57:45 -0500
Message-ID: <20030815045745.GA27468@wolff.to> (raw)
In-Reply-To: <sf3b6707.030@ems-t.ca>
References: <sf3b6707.030@ems-t.ca>

On Thu, Aug 14, 2003 at 10:40:02 -0400,
  Slawek Jarosz <Jarosz.S@ems-t.ca> wrote:
> Hi,
> 
>   I trying to write a query that will join 2 tables.   Here's the concept:
> Table 1: table1, primary key pk1
> Table 2: table2, primary key pk2
> 
> One of the fields (f2) in table2 contains either the primary key of table1 or a NULL value.  So normally a pretty basic query:
> 
> SELECT table1.*, table2.pk2 FROM table1, table2 WHERE table2.f2 = table1.pk1;  
> 
> BUT what I would like to do is show all records of Table 1 even if there is no match in Table 2.  Meaning that the reults could be 
> table1...   table2.pk2
> table1...   NULL
> 
> Doable?

You use outer joins to do this. Something like:
SELECT table1.*, table2.pk2 FROM
  table1 left join table2 on table2.f2 = table1.pk1;



view thread (5+ messages)  latest in thread

Message-ID: <20030815045745.GA27468@wolff.to>
Permalink:  ../20030815045745.GA27468@wolff.to/
Also on:    postgresql.org/message-id/20030815045745.GA27468@wolff.to

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: bruno@wolff.to, Jarosz.S@ems-t.ca
  Subject: Re: Optional join
  In-Reply-To: <20030815045745.GA27468@wolff.to>

* 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