pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jasen Betts <jasen@xnet.co.nz>
To: pgsql-sql@postgresql.org
Subject: Re: Updating one table with data from another
Date: 20 Aug 2009 10:51:13 GMT
Message-ID: <h6j9r1$7fc$1@reversiblemaps.ath.cx> (raw)
References: <a90cab78-877d-4f88-a3a4-0e569b3f55ef@z31g2000yqd.googlegroups.com>

On 2009-08-18, drew <iamdrewhayes@gmail.com> wrote:
> Hey all,
> There are two things I need to do:
> 1. Update existing rows with new data
> 2. Append new rows
>
> I need to update only some of the fields table1 with data from
> table2.  These tables have the exact same fields.
>
> So here's what I have currently for appending new rows (rows where CID
> does not currently exist in table1, but have been added to table2):
> INSERT INTO table1 (field1, field2, ...)
> SELECT field1, field2, ...
> FROM table2
> WHERE NOT EXISTS (SELECT CID FROM table1);
>
>
> But something is wrong with the logic there and I'm not quite getting
> it.

the where clause is wrong.


WHERE NOT EXISTS (SELECT 1 FROM table1 where table1.cid=table2.cid);

or 

WHERE NOT cid IN (SELECT CID FROM table1);





view thread (2+ messages)

Message-ID: <h6j9r1$7fc$1@reversiblemaps.ath.cx>
Permalink:  ../h6j9r1$7fc$1@reversiblemaps.ath.cx/
Also on:    postgresql.org/message-id/h6j9r1$7fc$1@reversiblemaps.ath.cx

 · 

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: jasen@xnet.co.nz
  Subject: Re: Updating one table with data from another
  In-Reply-To: <h6j9r1$7fc$1@reversiblemaps.ath.cx>

* 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