pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Unable to get UPDATE ... FROM syntax correct
6+ messages / 3 participants
[nested] [flat]

* Unable to get UPDATE ... FROM syntax correct
@ 2010-02-19 09:25  Gordon Ross <gr306@ucs.cam.ac.uk>
  0 siblings, 2 replies; 6+ messages in thread

From: Gordon Ross @ 2010-02-19 09:25 UTC (permalink / raw)
  To: pgsql-sql

I have two tables:

               Table "public.audit"
   Column   |            Type      | Modifiers
------------+----------------------+-----------
 id         | integer              | (serial)
 record_id  | integer              | not null
 key        | character varying    |
(...)


      Table "public.extension"
  Column |   Type     |  Modifiers
---------+------------+------------
 id      | integer    | (serial)
 number  | integer    |
(...)


The column "key" was recently added to the "audit" table, and I wish to
populate it with the value form the "number" column in the extension table.
The join between the tables is audit.record_id = extension.id

I tried:

UPDATE audit SET key = extension.number FROM extension WHERE audit.record_id
= extension.number;

But that returns saying "UPDATE 0"

However, doing:

SELECT audit.record_id, extension.number FROM audit, extension WHERE
audit.record_id = extension.id;

Works fine.

Can someone tell me what I'm doing wrong ?

Thanks,

GTG




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Unable to get UPDATE ... FROM syntax correct
@ 2010-02-19 10:28  zkn <zkn@abv.bg>
  parent: Gordon Ross <gr306@ucs.cam.ac.uk>
  1 sibling, 1 reply; 6+ messages in thread

From: zkn @ 2010-02-19 10:28 UTC (permalink / raw)
  To: pgsql-sql; +Cc: Gordon Ross <gr306@ucs.cam.ac.uk>

update audit set key = (select extension.number from extension where audit.record_id = extension_id)
On 19.02.2010, at 11:25, Gordon Ross wrote:

> I have two tables:
> 
>               Table "public.audit"
>   Column   |            Type      | Modifiers
> ------------+----------------------+-----------
> id         | integer              | (serial)
> record_id  | integer              | not null
> key        | character varying    |
> (...)
> 
> 
>      Table "public.extension"
>  Column |   Type     |  Modifiers
> ---------+------------+------------
> id      | integer    | (serial)
> number  | integer    |
> (...)
> 
> 
> The column "key" was recently added to the "audit" table, and I wish to
> populate it with the value form the "number" column in the extension table.
> The join between the tables is audit.record_id = extension.id
> 
> I tried:
> 
> UPDATE audit SET key = extension.number FROM extension WHERE audit.record_id
> = extension.number;
> 
> But that returns saying "UPDATE 0"
> 
> However, doing:
> 
> SELECT audit.record_id, extension.number FROM audit, extension WHERE
> audit.record_id = extension.id;
> 
> Works fine.
> 
> Can someone tell me what I'm doing wrong ?
> 
> Thanks,
> 
> GTG
> 
> 
> -- 
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
> 




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Unable to get UPDATE ... FROM syntax correct
@ 2010-02-19 10:32  Gordon Ross <gr306@ucs.cam.ac.uk>
  parent: zkn <zkn@abv.bg>
  0 siblings, 0 replies; 6+ messages in thread

From: Gordon Ross @ 2010-02-19 10:32 UTC (permalink / raw)
  To: pgsql-sql

On 19/02/2010 10:28, "zkn" <zkn@abv.bg> wrote:
> update audit set key = (select extension.number from extension where
> audit.record_id = extension_id)

No joy, I still get "UPDATE 0"

:-(

GTG




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Unable to get UPDATE ... FROM syntax correct
@ 2010-02-19 10:36  Richard Huxton <dev@archonet.com>
  parent: Gordon Ross <gr306@ucs.cam.ac.uk>
  1 sibling, 1 reply; 6+ messages in thread

From: Richard Huxton @ 2010-02-19 10:36 UTC (permalink / raw)
  To: Gordon Ross <gr306@ucs.cam.ac.uk>; +Cc: pgsql-sql

On 19/02/10 09:25, Gordon Ross wrote:
>
> UPDATE audit SET key = extension.number FROM extension WHERE audit.record_id
> = extension.number;
>
> But that returns saying "UPDATE 0"

Works here - are you sure you don't have any triggers interfering?

BEGIN;

CREATE TEMP TABLE audit (
     id        int,
     record_id int,
     key       varchar,
     PRIMARY KEY (id)
);

CREATE TEMP TABLE extension (
     id       int,
     number   int,
     PRIMARY KEY (id)
);

INSERT INTO audit VALUES (1,1,''), (2,2,''), (3,3,'');
INSERT INTO extension VALUES (-1,1), (-2,2), (-3,3);

UPDATE audit SET key = extension.number
FROM extension
WHERE audit.record_id = extension.number;

SELECT * FROM audit ORDER BY id;

ROLLBACK;

CREATE TABLE
INSERT 0 3
INSERT 0 3
UPDATE 3
  id | record_id | key
----+-----------+-----
   1 |         1 | 1
   2 |         2 | 2
   3 |         3 | 3


-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Unable to get UPDATE ... FROM syntax correct
@ 2010-02-19 10:58  Gordon Ross <gr306@ucs.cam.ac.uk>
  parent: Richard Huxton <dev@archonet.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Gordon Ross @ 2010-02-19 10:58 UTC (permalink / raw)
  To: pgsql-sql

On 19/02/2010 10:36, "Richard Huxton" <dev@archonet.com> wrote:

> Works here - are you sure you don't have any triggers interfering?

Doh ! *blush*

Yes, I did, to stop anyone tampering with the audit table ;-)

Thanks for that.

GTG




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Unable to get UPDATE ... FROM syntax correct
@ 2010-02-19 11:33  Richard Huxton <dev@archonet.com>
  parent: Gordon Ross <gr306@ucs.cam.ac.uk>
  0 siblings, 0 replies; 6+ messages in thread

From: Richard Huxton @ 2010-02-19 11:33 UTC (permalink / raw)
  To: Gordon Ross <gr306@ucs.cam.ac.uk>; +Cc: pgsql-sql

On 19/02/10 10:58, Gordon Ross wrote:
> On 19/02/2010 10:36, "Richard Huxton"<dev@archonet.com>  wrote:
>
>> Works here - are you sure you don't have any triggers interfering?
>
> Doh ! *blush*
>
> Yes, I did, to stop anyone tampering with the audit table ;-)

There you go - it worked :-)

-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2010-02-19 11:33 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-02-19 09:25 Unable to get UPDATE ... FROM syntax correct Gordon Ross <gr306@ucs.cam.ac.uk>
2010-02-19 10:28 ` zkn <zkn@abv.bg>
2010-02-19 10:32   ` Gordon Ross <gr306@ucs.cam.ac.uk>
2010-02-19 10:36 ` Richard Huxton <dev@archonet.com>
2010-02-19 10:58   ` Gordon Ross <gr306@ucs.cam.ac.uk>
2010-02-19 11:33     ` Richard Huxton <dev@archonet.com>

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