pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Johnston <polobo@yahoo.com>
To: 'ssylla' <stefansylla@gmx.de>
To: pgsql-sql@postgresql.org
Subject: Re: update column with multiple values
Date: Fri, 10 Feb 2012 11:08:11 -0500
Message-ID: <033b01cce80e$30313860$9093a920$@yahoo.com> (raw)
In-Reply-To: <1328754679303-5468395.post@n5.nabble.com>
References: <1328754679303-5468395.post@n5.nabble.com>

-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of ssylla
Sent: Wednesday, February 08, 2012 9:31 PM
To: pgsql-sql@postgresql.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.

Stefan 

---------------------------------------------------------------

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.







view thread (2+ messages)

Message-ID: <033b01cce80e$30313860$9093a920$@yahoo.com>
Permalink:  ../033b01cce80e$30313860$9093a920$@yahoo.com/
Also on:    postgresql.org/message-id/033b01cce80e$30313860$9093a920$@yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: polobo@yahoo.com, stefansylla@gmx.de
  Subject: Re: update column with multiple values
  In-Reply-To: <033b01cce80e$30313860$9093a920$@yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox