Re: updating records in table A from selected records in table B

From: Robert Poor <rdpoor(at)gmail(dot)com>
To: Maximilian Tyrtania <lists(at)contactking(dot)de>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: updating records in table A from selected records in table B
Date: 2011-03-30 16:22:56
Message-ID: AANLkTinxvcyuCm9WxppPn+u9MhePSq-G7_o9H5JMX-Yx@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

@Maximilian:

On Tue, Mar 29, 2011 at 22:46, Maximilian Tyrtania <lists(at)contactking(dot)de> wrote:
>> It seems that UPDATE is designed only to update one record at a time...
>
> Oh, no, it is certainly possible to do what you want here. The usual trick is:
>
> UPDATE sometable set somefield=somevalue where id in (select id from somecomplicatedsubquery)

I may be misreading your reply, but I get

PGError: ERROR: missing FROM-clause entry for table "candidate"

With the following query:

UPDATE table_as SET incumbent.value = candidate.value
WHERE id IN ( SELECT id
FROM table_b AS candidates
INNER JOIN table_as AS incumbents
ON incumbents.key = candidate.key)

Is that what you meant?

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Robert Poor 2011-03-30 18:16:06 Re: updating records in table A from selected records in table B [SOLVED]
Previous Message aaronenabs 2011-03-30 15:24:35 Re: pg_dumpall