dbTalk Databases Forums  

Re: [BUGS] Update with join ignores where clause - updates all rows

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss Re: [BUGS] Update with join ignores where clause - updates all rows in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] Update with join ignores where clause - updates all rows - 08-29-2004 , 11:12 AM






"Brian" <bengelha (AT) comcast (DOT) net> writes:
Quote:
The same join here, updates every row in the table which is incorrect.

update
dw.prints_by_hour
set
count = h.count + w.count
from
dw.prints_by_hour_work w , dw.prints_by_hour h
WHERE
w.year = h.year
and w.month = h.month
and w.day = h.day
and w.hour = h.hour
No, it's not a bug: it's a self-join. If we identified the target table
with the "h" table then it would be impossible to do self-joins in
UPDATE.

You need to write

update
dw.prints_by_hour
set
count = dw.prints_by_hour.count + w.count
from
dw.prints_by_hour_work w
WHERE
w.year = dw.prints_by_hour.year
and w.month = dw.prints_by_hour.month
and w.day = dw.prints_by_hour.day
and w.hour = dw.prints_by_hour.hour

There's been some talk of allowing an alias to be attached to the target
table ("update dw.prints_by_hour h") which would make it possible to
write the update a bit more compactly, but we haven't done that.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.