Re: Question About UNION

From: Bill Thoen <bthoen(at)gisnet(dot)com>
To: David Wilson <david(dot)t(dot)wilson(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Question About UNION
Date: 2008-10-09 19:31:39
Message-ID: 48EE5C1B.9090101@gisnet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David Wilson wrote:
> On Thu, Oct 9, 2008 at 1:48 PM, Bill Thoen <bthoen(at)gisnet(dot)com> wrote:
>
>> No, this won't work. Here's an example of my tables:
>> Table1
>> 1, 12, A
>> 2, 16, B
>> 8, 6, A
>> 19, 9, C
>>
>> Table2
>> 1, 13, D
>> 2, 16, B
>> 8, 6, B
>> 12, 5, A
>>
>
> select * from table1
> union
> select table2.* from table2 left join table1 on table2.a=table1.a and
> table2.b=table1.b where table2.a is null;
>
> (Written in gmail, but you should get the basic idea.)
>
Thanks, but that didn't work. That selected only the records from
table1. However, this did work:
CREATE TABLE table3 AS
SELECT * FROM table1;

CREATE UNIQUE INDEX table3_pk ON table3 (a, b);

INSERT INTO table3 SELECT * FROM table2
WHERE NOT EXISTS (SELECT a, b FROM table1 t1
WHERE table2.a=t1.a AND table2.b=t1.b);

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Wilson 2008-10-09 19:35:32 Re: Question About UNION
Previous Message Josh Williams 2008-10-09 19:22:28 Re: Question About UNION