Re: select from multiple tables

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: e-letter <inpost(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: select from multiple tables
Date: 2012-06-12 20:04:03
Message-ID: 4FD7A0B3.9050007@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 06/12/2012 11:16 AM, e-letter wrote:
> Readers,
>
> A database has table1, table2, table3, table4 and each table has
> columns of text and integers:
>
> table1
> text1, 10
> text2, 20
> text3, 30
> text4, 40
> text5, 50
> text6, 60
> text7, 70
> text8, 80
>
> table2
> text1, 10
> text2, 20
>
> table3
> text3, 30
> text4, 40
>
> table4
> text5, 50
>
> Is it possible to create a new table (table5) by creating a query that
> selects rows that _both_ exist in table1 _and_ are not in any of the
> preceding tables (table2, table3, table4)?
>
> Thanks in advance.
>

create table table5 as
select textcol, intcol from table1 except
(
select textcol, intcol from table2 union
select textcol, intcol from table3 union
select textcol, intcol from table3
);

Cheers,
Steve

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Richard Terry 2012-06-12 22:12:14 inserting a column into a view
Previous Message e-letter 2012-06-12 18:16:24 select from multiple tables