From: | "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com> |
---|---|
To: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Add column by using SELECT statement |
Date: | 2009-02-25 06:33:59 |
Message-ID: | 20090225063359.GA13273@a-kretschmer.de |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
In response to John Zhang :
> Hi all,
>
> I was wondering how I can add a column and populate it by some query.
>
> For example:
> TblA (Id, fld1)
> TblB(Id, fld1, fld2)
>
> I have a query:
> SELECT b.fld2
> FROM tblB b
> WHERE condition1
>
> what I want to do is add a column in tblA: fld2
> and polpulate the newly added field with the query
> on tblA.Id=tblB.Id
>
> Any advice? Any input would be much appreciated.
ALTER TABLE and UPDATE:
test=# create table tabla (id int, f1 int);
CREATE TABLE
test=*# create table tablb (id int, f1 int, f2 int);
CREATE TABLE
test=*# insert into tabla values (1,1);
INSERT 0 1
test=*# insert into tabla values (2,2);
INSERT 0 1
test=*# insert into tabla values (3,3);
INSERT 0 1
test=*# insert into tablb values (1,1,1);
INSERT 0 1
test=*# insert into tablb values (2,2,2);
INSERT 0 1
test=*# insert into tablb values (3,3,3);
INSERT 0 1
test=*# alter table tabla add column f2 int;
ALTER TABLE
test=*# commit;
COMMIT
test=# update tabla set f2= tablb.f2 from tablb where tabla.id=tablb.id;
UPDATE 3
test=*# select * from tabla;
id | f1 | f2
----+----+----
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
(3 rows)
HTH, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
From | Date | Subject | |
---|---|---|---|
Next Message | Steve Midgley | 2009-02-25 18:42:49 | Re: Best practices for geo-spatial city name searches? |
Previous Message | John Zhang | 2009-02-25 03:37:54 | Add column by using SELECT statement |