Re: Update from select

From: Igor Neyman <ineyman(at)perceptron(dot)com>
To: "bret_stern(at)machinemanagement(dot)com" <bret_stern(at)machinemanagement(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Update from select
Date: 2013-05-13 20:33:42
Message-ID: A76B25F2823E954C9E45E32FA49D70EC1B7CED04@mail.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> PG 8.4
>
> Having trouble putting together an update query to update multiple
> columns in tbl1 from columns in tbl2.
>
> update tbl1
> set col3,col4,col5
> from
> (select col3, col4,col5 from tbl2 where col1="criteria")
>
>
>
> Can someone add to the Postgres Docs (shown below) to help me with
> this.
>
> UPDATE employees SET sales_count = sales_count + 1 WHERE id =
> (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
>
>
> Many thanks
>
> Bret Stern

Your question isn't very clear.
Are you updating all records in tbl1?
Or col1 in tbl1 should also match col1 in tbl2?

If that's the case:

UPDATE tbl1 t1
SET col3 = t2.col3, col4 = t2.col4, col5 = t2.col5
FROM tbl2 t2
WHERE t1.col1 = t2.col1
AND t.col1 = "criteria";

Igor Neyman

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Janes 2013-05-13 20:49:17 Re: refactoring a database owner without "reassign owned"
Previous Message Justin Tocci 2013-05-13 20:28:43 Re: Update from select