Re: Update Query Problem

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Update Query Problem
Date: 2008-10-10 11:58:00
Message-ID: 20081010115800.GC12421@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

am Fri, dem 10.10.2008, um 12:38:24 +0100 mailte Jeng Yu folgendes:
> Hi People!
>
> I'm doing an application and I've chosen postgresql
> for the backend db. I need to use SQL update command
> like this in my application:
>
> update mytable set x='20' where id='someid' order by
> id limit 1;

Du you have a primary key?

If you don't have a pk, you can use the ctid:

test=# create table a ( id int, val int);
CREATE TABLE
test=# insert into a values (1,1);
INSERT 0 1
test=# insert into a values (1,1);
INSERT 0 1
test=# insert into a values (1,1);
INSERT 0 1
test=# select * from a;
id | val
----+-----
1 | 1
1 | 1
1 | 1
(3 rows)

test=# begin;
BEGIN
test=# update a set val=2 where ctid=(select ctid from a where id=1 limit 1);
UPDATE 1
test=# select * from a;
id | val
----+-----
1 | 1
1 | 1
1 | 2
(3 rows)

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-10-10 12:24:35 Re: flood in logs
Previous Message hubert depesz lubaczewski 2008-10-10 11:54:19 Re: flood in logs