List:General Discussion« Previous MessageNext Message »
From:Gary Smith Date:December 27 2009 6:30pm
Subject:Re: last_insert_id
View as plain text  
Victor Subervi wrote:
> On Sun, Dec 27, 2009 at 12:00 PM, Michael Dykman <mdykman@stripped> 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
Thread
last_insert_idVictor Subervi27 Dec
  • Re: last_insert_idMattia Merzi27 Dec
    • Re: last_insert_idVictor Subervi27 Dec
      • Re: last_insert_idMichael Dykman27 Dec
        • Re: last_insert_idVictor Subervi27 Dec
          • Re: last_insert_idGary Smith27 Dec
            • Re: last_insert_idVictor Subervi27 Dec
            • Re: last_insert_idCarsten Pedersen27 Dec
  • Re: last_insert_idSteve Edberg27 Dec
    • Re: last_insert_idGary Smith27 Dec
      • Re: last_insert_idMark Goodge27 Dec