Re: join if there, blank if not

From: Jacques Williams <jacques(at)jacqro(dot)COM>
To: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: join if there, blank if not
Date: 2000-07-12 14:41:12
Message-ID: 20000712104112.A2531@jacqro3.jacqro.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Gary,

What you want here is an outer join. The syntax would look something like this:

select c.cdate, c.ctime, c.cextn, c.cnumber, n.ndesc
from calls c, numbers n
where c.cnumber=n.nnumber
union all
select null as cdate, null as cextn, null as cnumber, n.nnumber, n.ndesc
from numbers;

(I haven't tried this, but it should work.) For more information on outer joins, see Bruce Momjian's book at http://www.postgresql.org/docs/aw_pgsql_book/ .

Jacques Williams

On Wed, Jul 12, 2000 at 04:34:04PM +0100, Gary Stainburn wrote:
> Hi all,
>
> I just can't get my head round this one so I hope one of you can.
>
> I've got two tables, one holding phone calls, and another holding phone numbers.
>
> I want to do a select where if the number in the calls table exists
> in the numbers table, the description is included otherwise the
> description field is blank.
>
> Unfortunately, using the select I've tried, if the number is not on
> the one of the tables, it's ignored.
>
> Calls table
> cdate date
> ctime time
> cextn char(3)
> cnumber x(12)
>
> Numbers table
>
> nnumber x(12)
> ndesc x(30)
>
> Select I tried.
>
> select c.cdate, c.ctime, c.cextn, c.cnumber n.ndesc
> from calls c, numbers n where c.cnumber = n.nnumber;
>
> -----------------------------------------
> Gary Stainburn.
> Work: http://www.ringways.co.uk mailto:gary(dot)stainburn(at)ringways(dot)co(dot)uk
> REVCOM: http://www.revcom.org.uk mailto:gary(dot)stainburn(at)revcom(dot)org(dot)uk
> -----------------------------------------
> Murphy's Laws: (327) The minute before the engineer arrives, the printer starts working.
> -----------------------------------------
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Gary Stainburn 2000-07-12 15:34:04 join if there, blank if not
Previous Message Jean-Marc Libs 2000-07-12 14:02:35 Re: SERIAL type does not generate new ID ?