OSEC

Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
Re: last_insert_id

From: Gary Smith (listsl33t-d00d.co.uk)
Date: Sun Dec 27 2009 - 12:30:17 CST


Victor Subervi wrote:
> On Sun, Dec 27, 2009 at 12:00 PM, Michael Dykman <mdykmangmail.com> wrote:
>
>
>> last_insert_id() returns the last id auto-incremented in *the current
>> session*. If you disconnect and reconnect, it can not be retrieved.
>>
>>
>
> Ahah! So how do I retrieve the last id inserted irrespective of connection?
>
Would max() work for you? This isn't necessarily foolproof, as it would
show the highest ID if you used max(id), for instance - this won't
necessarily be what you were expecting, but in most cases will be what
you'd imagine it would be.

An example of where it wouldn't be: Although ID is auto_increment, you
could define a row as, say, '10005583429'. This would be a valid input.
Selecting max(id) would return that number. However, auto_increment
wouldn't change - it would still be '34' (or whatever) for the next
line. Thus, max(id) would be wrong for however long it takes for
auto_increment to get to that figure, which could potentially be a long
time.

Cheers,

Gary

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql