Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 1B89C6327F2 for ; Thu, 20 Aug 2009 07:51:40 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14406-06 for ; Thu, 20 Aug 2009 10:51:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from titania.wxnz.net (titania.wxnz.net [58.28.4.13]) by mail.postgresql.org (Postfix) with ESMTP id 446A46325F1 for ; Thu, 20 Aug 2009 07:51:29 -0300 (ADT) Received: from gonzo (ip-118-90-131-0.xdsl.xnet.co.nz [118.90.131.0]) by titania.wxnz.net (Postfix) with ESMTP id F06331098198 for ; Thu, 20 Aug 2009 22:51:25 +1200 (NZST) Received: from news by gonzo with local (Exim 4.69) (envelope-from ) id 1Me5Ef-000248-HC for pgsql-sql@postgresql.org; Thu, 20 Aug 2009 22:51:13 +1200 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: Updating one table with data from another Date: 20 Aug 2009 10:51:13 GMT Organization: Dis (not Dat) Organisation Lines: 30 Message-ID: References: NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: reversiblemaps.ath.cx 1250765473 7660 127.0.0.1 (20 Aug 2009 10:51:13 GMT) X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-2 (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185, RCVD_IN_DNSWL_LOW=-1 X-Spam-Level: X-Archive-Number: 200908/110 X-Sequence-Number: 33019 On 2009-08-18, drew 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);