The good ol' LAST_INSERT_ID() function is what you probably need. Check it
out in manual.

Lian

> -----Original Message-----
> From: Matt Hyne [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, June 24, 2003 5:58 AM
> To: [EMAIL PROTECTED]
> Subject: Getting the unique auto-increment primary key after an insert
>
>
>
> Hi Folks, needs a little advice.
>
> I have a table that contains a unique auto-incrementing primary key
> called 'id' (eg 1,2,3,4...).   When inserting data, I let mySQL allocate
> the value that goes into this field.
>
> Now I want to insert a new row into this table and get the 'id' value
> that mySQL has allocated (so I can then do some more work such as
> attaching a filename called 'id'.xxx).
>
> Can someone suggest a good way to do this ?  I thought of maybe doing a
> "SELECT MAX(id) FROM table" first and then using id+1 as the new 'id'
> for the next entry but this is frought with danger as someone could come
> along and insert a new entry between my SELECT and INSERT.
>
> Because 'id' is the only guaranteed unique column, I cannot think of a
> way to do a SELECT after an INSERT to get back the 'id' of the last
> thing inserted.
>
> Can anyone help ?
>
> Matt
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]
>
>


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to