How to update a newly added column with sub selects?

From: "J(dot)V(dot)" <jvsrvcs(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: How to update a newly added column with sub selects?
Date: 2012-04-27 21:22:10
Message-ID: 4F9B0E02.7040601@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I need to add a new column to a table (nullable), then populate and then
add a not null constraint.

The value of the new column is obtained by doing three or more nested
sub-selects to get the id that should go into this column. At this
point I can add a not null and foreign key constraint.

Ideally would like to do this with a single updated statement, but not
sure how:

So for example, given a table, I have to select the id from that table,
and for each id, pull id's from the next table, and from there use that
id for the next and so on.

select id from table; is the id I am starting with, so this might show

1
2
3
4

update table set new_column_id = (select id2 from join_table2 where
new_column_id=2);

but I do not want to write a loop and iterate through this stament
passing 1,2,3,4 to the above statement, just a single statement.

Is this possible?

thanks

J.v.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Josh Kupershmidt 2012-04-27 22:27:05 Re: How to get the all the activities running during specific time range.Thanks!Plese help!
Previous Message Alexander Farber 2012-04-27 20:25:23 Re: How to add "on delete cascade" constraints