dbTalk Databases Forums  

newbie question on update

comp.databases comp.databases


Discuss newbie question on update in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Steven Paul
 
Posts: n/a

Default newbie question on update - 06-12-2007 , 03:13 PM






I have a clients table which consists of, among other things, a column
named source.

I have a second table, sources, with two columns - client_id and source.

The are fewer rows in sources than in clients. I am trying to update
clients.source with sources.source without affecting the clients that
do not have a corresponding row in sources.

My first try was update clients set clients.source = sources.source
where clients.client_id = sources.client_id

But I ended up with nulls in the clients.source column for rows in
which there was no row in sources.

I am guessing that I need to involve a join somehow, but I am having a
very hard time not thinking procedurally, and just don't "get" it.

I've been looking at Join examples, but anything non-trivial just
baffles me. Would someone be so kind as to explain how to do what I'm
trying to do?

Thanks.

-Steve

Reply With Quote
  #2  
Old   
Steven Paul
 
Posts: n/a

Default Re: newbie question on update - 06-12-2007 , 09:07 PM






In article <466f16f2$0$90276$14726298 (AT) news (DOT) sunsite.dk>, Nis Jørgensen
<nis (AT) superlativ (DOT) dk> wrote:

Quote:
Since you do not write which database you use, i will assume you are
using postgresql 8.1. In that case you can do

UPDATE clients SET clients.source = sources.source
FROM sources
WHERE clients.client_id = sources.client_id

An approach which should work in any db that supports subselects:

UPDATE clients SET clients.source = (SELECT source FROM sources WHERE
clients.client_id = sources.client_id)
WHERE client_id in (SELECT client_id FROM sources)

Hope these helps
Yes it does. Thank you very much.
So let me try to understand how it works (using the second version).
First it finds the set of clients who have a corresponding row in
sources (the 'outside' part of the statement), then it updates the
column using the 'inner' select?

I am finding it difficult to think in terms of sets and 'simultaneous'
actions. I keep wanting to solve these things procedurally.

Thanks again.


Reply With Quote
  #3  
Old   
--CELKO--
 
Posts: n/a

Default Re: newbie question on update - 06-14-2007 , 11:43 PM



Quote:
I am finding it difficult to think in terms of sets and 'simultaneous' actions. I keep wanting to solve these things procedurally.
It takes about a year to have the revelation My next book will
deal with thinking in sets. Hey, remember how weird recurson was?



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.