pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ericson Smith <eric@did-it.com>
To: Josh Goldberg <josh@4dmatrix.com>
Cc: Michael Kovalcik <makd32@yahoo.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: How do I select the last Id in a column???
Date: Thu, 26 Jun 2003 17:10:04 -0400
Message-ID: <3EFB612C.50800@did-it.com> (raw)
In-Reply-To: <01f401c33c25$fbffd5d0$6e02a8c0@4dmatrix.com>
References: <20030623184706.75021.qmail@web11101.mail.yahoo.com>
	<01f401c33c25$fbffd5d0$6e02a8c0@4dmatrix.com>

To get the ID after the fact:
   SELECT currval('my_seq') AS id;

To get the ID before even inserting it
   SELECT nextval('my_seq') AS id;


- Ericson Smith
eric@did-it.com


Josh Goldberg wrote:

>to get the last record inserted just
>select * from row order by autoincId desc limit 1;
>
>if you have several clients doing this you might want to put your insert and
>the select inside a transaction.
>
>----- Original Message -----
>From: "Michael Kovalcik" <makd32@yahoo.com>
>To: <pgsql-admin@postgresql.org>
>Sent: Monday, June 23, 2003 11:47 AM
>Subject: [ADMIN] How do I select the last Id in a column???
>
>
>  
>
>>Hello,
>>
>>I'm trying to find out how I select the last id in a
>>column if I don't know anything else about the row.
>>What i am doing is inserting information into my
>>Orders table and the id is auto-incrementing.  The
>>next thing i want to be able to do is grab the the id
>>of the order i just inserted.  How do i do this?
>>
>>Here is the code i have so far:
>>
>>my $sql = "INSERT INTO ${Orders}
>>(locationid,shippingid,statusid,date) VALUES (?
>>,?,?,?)";
>>
>>my $sth = $dbh->prepare($sql);
>>
>>$sth->execute(${LocationID},${ShippingID},${StatusID},${date});
>>#--*************************************
>>my $sth = $dbh->prepare("SELECT id FROM ${Orders}");
>>
>>$sth->execute();
>>$OrderID = $sth->fetchrow_arrayref;
>>
>>I know the select statement that i have will not work,
>>because it will bring me every id number in the table.
>> I just want the latest one.....
>>
>>Please HELP............
>>
>>Thank you so much,
>>Mike
>>
>>__________________________________
>>Do you Yahoo!?
>>SBC Yahoo! DSL - Now only $29.95 per month!
>>http://sbc.yahoo.com
>>
>>---------------------------(end of broadcast)---------------------------
>>TIP 2: you can get off all lists at once with the unregister command
>>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>>
>>    
>>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>  
>




view thread (13+ messages)  latest in thread

Message-ID: <3EFB612C.50800@did-it.com>
Permalink:  ../3EFB612C.50800@did-it.com/
Also on:    postgresql.org/message-id/3EFB612C.50800@did-it.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: eric@did-it.com, josh@4dmatrix.com, makd32@yahoo.com
  Subject: Re: How do I select the last Id in a column???
  In-Reply-To: <3EFB612C.50800@did-it.com>
  References: <20030623184706.75021.qmail@web11101.mail.yahoo.com> <01f401c33c25$fbffd5d0$6e02a8c0@4dmatrix.com> <3EFB612C.50800@did-it.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