Re: need clean way to copy col vals from one rec to another

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: "Gauthier, Dave" <dave(dot)gauthier(at)intel(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: need clean way to copy col vals from one rec to another
Date: 2010-02-10 16:46:19
Message-ID: 9EDAAEF4-A91B-4E05-9A7D-4D9BD69A256C@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10 Feb 2010, at 17:28, Gauthier, Dave wrote:

> create table foo (name text, company text, job text);
> insert into foo (name,company,job) values (‘joe’,’ge’,’engineer’);
> insert into foo (name) values (‘sue’);
>
> What I want to do is map joe’s company and job over to the sue record, ending up with....
> ‘sue’ ‘ge’ ‘engineer’
>
> Is there a quick/clever.efficient way to do this?

UPDATE foo SET company = joe.company, job = joe.job
FROM foo AS joe
WHERE foo.name = 'sue'
AND joe.name = 'joe';

You could also do this on insert by using:

INSERT INTO foo (name, company, job)
SELECT 'sue', joe.company, joe.job
FROM foo AS joe
WHERE joe.name = 'joe';

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4b72e2dd10446151245148!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gauthier, Dave 2010-02-10 17:05:29 Re: need clean way to copy col vals from one rec to another
Previous Message Vincenzo Romano 2010-02-10 16:40:25 Re: Best way to handle multi-billion row read-only table?