Skip site navigation (1) Skip section navigation (2)

Re: update column with multiple values

From: "David Johnston" <polobo(at)yahoo(dot)com>
To: "'ssylla'" <stefansylla(at)gmx(dot)de>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: update column with multiple values
Date: 2012-02-10 16:08:11
Message-ID: 033b01cce80e$30313860$9093a920$ (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org [mailto:pgsql-sql-owner(at)postgresql(dot)org]
On Behalf Of ssylla
Sent: Wednesday, February 08, 2012 9:31 PM
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] update column with multiple values

Dear list,

sorry, I already posted this, but it did not seem to have been accepted by
the mailing list. So here's my second try:

I need to update all rows of a column ('parent') based on a subquery that
returns me the first four digits of another column ('id'):

UPDATE admin SET parent=(SELECT SUBSTR(id,1,4) FROM admin);

After executing, I get the following error-message:

"more than one row returned by a subquery used as an expressionmore than one
row returned by a subquery used as an expression"

I am not quite sure about that, but maybe I need to construct a
function/loop to fulfill this task?

thanks for help.



No sub-select required, just use the function:

UPDATE admin SET parent = substr(id,1,4);

This will, for each record, set the value of parent to the first four
characters of its ID.

When you use the sub-select there is not inherent linkage between the
"UPDATE" table and the "FROM" table.  You can make a correlated sub-query
but in this case the is necessary.

David J.

In response to

pgsql-sql by date

Next:From: Jasen BettsDate: 2012-02-11 03:18:43
Subject: Re: time interval math
Previous:From: Oliveiros d'Azevedo CristinaDate: 2012-02-10 15:33:09
Subject: Re: update column

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group