dan wrote:
Hi,

I've looked around to not much avail, what I'm looking for is a SELECT query
that selects the last record in a database. Each table has the primary field
"id", to which auto increments on each input. I don't want to have to load
the entire db, cycle through it until I find the last one, the program needs
to react fast, not being slowed down by having to cycle through database
records.

Basically what happens is the program enters information using INSERT, and
then it needs to re-read the data it's just sent to verify it, and get the
id number that auto_increment assigned it.

Is this possible? Does this make sense? *laugh*


Depending on your database there may be a method to get the inserted id from the actual insert statement. If not, then this can be dangerous unless you do so within a transaction so that both operations are atomic, so as to prevent an insert to be followed by another then the first select, then a second select, which provides the same "last" id, but not the proper one to both selects. Having said that, if you want the last id, then assuming the id is a primary key you should be able to select 1 row and order by the id descending and this should be very fast.


If you don't care overwhelmingly about portability I would check the DBD docs for your particular DB (assuming you are using the DBI) to see if there is a way to get the insert id, otherwise I would look through the archives of the dbi-users list as this a fairly frequently asked question....

http://danconia.org


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to