Re: How can I merge two tables? - Mailing list pgsql-general

From Jeff Davis
Subject Re: How can I merge two tables?
Date
Msg-id 1315015688.8877.20.camel@jdavis-ux.asterdata.local
Whole thread Raw
In response to How can I merge two tables?  (Jerry LeVan <jerry.levan@gmail.com>)
List pgsql-general
On Thu, 2011-09-01 at 13:09 -0400, Jerry LeVan wrote:
> As time goes by the tables on the various computers get out of
> sync.
>
> Is there an elegant way I can get all of the differences (uniquely)
> merged into a single table?

You can try a query involving NOT EXISTS, combined with dblink:

http://www.postgresql.org/docs/current/static/dblink.html

Effectively the query would be something like:

INSERT INTO registrations
 SELECT * FROM
  -- fetch remote version of table
  dblink(..., "SELECT * FROM registrations") AS remote_reg(...)
 WHERE NOT EXISTS
  (SELECT 1 FROM registrations local_reg
   WHERE local_reg.id = remote_reg.id);

(disclaimer: I didn't test this query out, it's just for illustrating
the idea).

Regards,
    Jeff Davis


pgsql-general by date:

Previous
From: Rich Shepard
Date:
Subject: Re: Looking for an intro-to-SQL book which is PostgreSQL-friendly
Next
From: Bret Fledderjohn
Date:
Subject: Re: Looking for an intro-to-SQL book which is PostgreSQL-friendly