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

Re: Get last serial (autoincrement) value

From: Frank Bax <fbax(at)sympatico(dot)ca>
To: pgsql-php(at)postgresql(dot)org
Subject: Re: Get last serial (autoincrement) value
Date: 2003-09-01 18:48:30
Message-ID: 5.2.1.1.0.20030901144559.00a0c3e0@pop6.sympatico.ca (view raw or flat)
Thread:
Lists: 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');


In response to

pgsql-php by date

Next:From: Bruno Wolff IIIDate: 2003-09-01 19:01:22
Subject: Re: Get last serial (autoincrement) value
Previous:From: David BusbyDate: 2003-09-01 18:11:06
Subject: Re: Get last serial (autoincrement) value

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