pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Get last serial (autoincrement) value
4+ messages / 4 participants
[nested] [flat]

* Get last serial (autoincrement) value
@ 2003-09-01 10:34  Andrei Verovski (aka MacGuru) <andreil1@mail.starlett.lv>
  0 siblings, 3 replies; 4+ messages in thread

From: Andrei Verovski (aka MacGuru) @ 2003-09-01 10:34 UTC (permalink / raw)
  To: pgsql-php

Hi,

Anyone knows how to obtain last inserted serial (autoincrement) id? I 
am currently using "SELECT id FROM table_name ORDER BY id DESC", and 
then retrieve first row and desired value.

May be there is more elegant way? I am using ADODB. ADODB has function 
which in theory allows to obtain
last inserted serial (autoincrement) id, but for Postgres it return OID.

This is from ADODB manual:

Insert_ID( )
Returns the last autonumbering ID inserted. Returns false if function 
not supported.
Only supported by databases that support auto-increment or object id's, 
such as PostgreSQL, MySQL and MSSQL currently. PostgreSQL returns the 
OID, which can change on a database reload.


Thanks in advance for any suggestion.


*********************************************
*   Best Regards   ---   Andrei Verovski
*
*   Personal Home Page
*   http://snow.prohosting.com/guru4mac
*   Mac, Linux, DTP, Development, IT WEB Site
*********************************************




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

* Re: Get last serial (autoincrement) value
@ 2003-09-01 18:11  David Busby <busby@pnts.com>
  parent: Andrei Verovski (aka MacGuru) <andreil1@mail.starlett.lv>
  2 siblings, 0 replies; 4+ messages in thread

From: David Busby @ 2003-09-01 18:11 UTC (permalink / raw)
  To: Andrei Verovski (aka MacGuru) <andreil1@mail.starlett.lv>; pgsql-php

select currval('table_id_column_seq');


----- Original Message ----- 
From: "Andrei Verovski (aka MacGuru)" <andreil1@mail.starlett.lv>
To: <pgsql-php@postgresql.org>
Sent: Monday, September 01, 2003 03:34
Subject: [PHP] Get last serial (autoincrement) value


> Hi,
> 
> Anyone knows how to obtain last inserted serial (autoincrement) id? I 
> am currently using "SELECT id FROM table_name ORDER BY id DESC", and 
> then retrieve first row and desired value.
> 
> May be there is more elegant way? I am using ADODB. ADODB has function 
> which in theory allows to obtain
> last inserted serial (autoincrement) id, but for Postgres it return OID.
> 
> This is from ADODB manual:
> 
> Insert_ID( )
> Returns the last autonumbering ID inserted. Returns false if function 
> not supported.
> Only supported by databases that support auto-increment or object id's, 
> such as PostgreSQL, MySQL and MSSQL currently. PostgreSQL returns the 
> OID, which can change on a database reload.
> 
> 
> Thanks in advance for any suggestion.
> 
> 
> *********************************************
> *   Best Regards   ---   Andrei Verovski
> *
> *   Personal Home Page
> *   http://snow.prohosting.com/guru4mac
> *   Mac, Linux, DTP, Development, IT WEB Site
> *********************************************
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that your
>       message can get through to the mailing list cleanly



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

* Re: Get last serial (autoincrement) value
@ 2003-09-01 18:48  Frank Bax <fbax@sympatico.ca>
  parent: Andrei Verovski (aka MacGuru) <andreil1@mail.starlett.lv>
  2 siblings, 0 replies; 4+ messages in thread

From: Frank Bax @ 2003-09-01 18:48 UTC (permalink / raw)
  To: pgsql-php

At 06:34 AM 9/1/03, aka MacGuru wrote:
>Anyone knows how to obtain last inserted serial (autoincrement) id? I am 
>currently using "SELECT id FROM table_name ORDER BY id DESC", and then 
>retrieve first row and desired value.


Faster then your version:
SELECT id FROM table_name ORDER BY id DESC   LIMIT 1

The 'elegant' solution:
SELECT currval('tablename_sequence_id');




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

* Re: Get last serial (autoincrement) value
@ 2003-09-01 19:01  Bruno Wolff III <bruno@wolff.to>
  parent: Andrei Verovski (aka MacGuru) <andreil1@mail.starlett.lv>
  2 siblings, 0 replies; 4+ messages in thread

From: Bruno Wolff III @ 2003-09-01 19:01 UTC (permalink / raw)
  To: Andrei Verovski <andreil1@mail.starlett.lv>; +Cc: pgsql-php

On Mon, Sep 01, 2003 at 13:34:32 +0300,
  Andrei Verovski <andreil1@mail.starlett.lv> wrote:
> 
> Anyone knows how to obtain last inserted serial (autoincrement) id? I 
> am currently using "SELECT id FROM table_name ORDER BY id DESC", and 
> then retrieve first row and desired value.

How you want to do this, depends on what you what to do with the number.

Normally if you just inserted a record that would create a new id and you
want to use that id to enter in another table, then you want to call
currval.



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


end of thread, other threads:[~2003-09-01 19:01 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-09-01 10:34 Get last serial (autoincrement) value Andrei Verovski (aka MacGuru) <andreil1@mail.starlett.lv>
2003-09-01 18:11 ` David Busby <busby@pnts.com>
2003-09-01 18:48 ` Frank Bax <fbax@sympatico.ca>
2003-09-01 19:01 ` Bruno Wolff III <bruno@wolff.to>

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