From: | Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com> |
---|---|
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 06:22:58 |
Message-ID: | 20021021232029.Y81472-100000@megazone23.bigpanda.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Mon, 21 Oct 2002, Patrick Nelson wrote:
> db1 (one)
> id varchar(5)
> dat varchar(8)
>
> db2 (many)
> id varchar(5)
> dat varchar(8)
>
> 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;
>
> Which seemed to have worked, but I'm not sure I've covered always getting
> the min value of multiple db2.dat for a given db1.id although my sub seems
> to have produced that ends I'm not confident. Any comments? Anecdotes?
I believe you need an order by in order to guarantee the minimum value.
order by b.id, b.dat would probably do it.
From | Date | Subject | |
---|---|---|---|
Next Message | Shridhar Daithankar | 2002-10-22 06:42:08 | Re: Flyer, Press Release |
Previous Message | Patrick Nelson | 2002-10-22 06:01:50 | Updating based on a join |