Re: is it possible to do an update with a nested select that references the outer update ?

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Jonathan Vanasco <postgres(at)2xlp(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: is it possible to do an update with a nested select that references the outer update ?
Date: 2009-10-06 17:48:16
Message-ID: 2E7FA7EA-3237-4C76-9B9C-CD0F7AF90986@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 6 Oct 2009, at 18:57, Jonathan Vanasco wrote:

> i think this attempt most clearly expresses what I was trying to do
>
> UPDATE table_a a set id_field = ( SELECT id_field FROM table_b b
> WHERE a.first_name || ' ' || b.last_name = a.fullname ) WHERE
> id_field IS NULL ;
>
> I'd be greatful if anyone has a pointer

You're looking for UPDATE FROM:

UPDATE table_a SET id_field = table_b.id_field FROM table_b WHERE
table_a.firstname || ' ' || table_b.last_name = table_a.fullname AND
table_a.id_field IS NULL;

Apparently table_a.id_field is not a primary key? Seems a bit weird to
me, but if it floats your boat...

Alban Hertroys

--
Screwing up is the best way to attach something to the ceiling.

!DSPAM:737,4acb82e311687224899625!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sachin Srivastava 2009-10-06 18:00:54 initdb failing
Previous Message Filip Rembiałkowski 2009-10-06 17:41:26 Re: Wishlist of PL/Perl Enhancements for PostgreSQL 8.5