Re: Updating based on a join

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Patrick Nelson <pnelson(at)neatech(dot)com>
Cc: "PostgreSQL List (E-mail)" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Updating based on a join
Date: 2002-10-22 13:55:28
Message-ID: 18376.1035294928@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Patrick Nelson <pnelson(at)neatech(dot)com> writes:
> My update is:

> UPDATE db1
> SET dat=(SELECT MIN(dat) FROM db2 WHERE id='A')
> WHERE id='A';

> Which does exactly one record in db1 and I would like them all done. So I
> used the FROM clause like:

> UPDATE db1
> SET dat=c.dat
> FROM (SELECT DISTINCT ON (b.id) a.sym, b.dat FROM db1 a, db2 b WHERE
> a.id=b.id) AS c
> WHERE db1.id=c.id;

Seems messy. Why not

UPDATE db1
SET dat = (SELECT MIN(b.dat) FROM db2 b WHERE db1.id = b.id);

I think this is actually SQL-standard, as well as being more readable;
the form with FROM is definitely not standard.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alice Lottini 2002-10-22 14:15:57 looking for documentation about the optimizer
Previous Message pilsl 2002-10-22 13:51:43 pgacess: cant find libpgctl (no faq !)