MySQL Forums
Forum List  »  MyISAM

Re: LAST_INSERT_ID() AND UPDATE STATEMENTS
Posted by: KimSeong Loh
Date: May 27, 2007 04:38PM

last_insert_id() has always been available after an INSERT to a table with auto_increment, no matter which version. Since UPDATE does not change the auto_increment value.

When you perform an UPDATE statement, you already know exactly which rows are updated since you provide the condition to choose the row using the WHERE clause in the update statement. There is no need to know the auto_increment value at all, use the same condition in a select to find the row. Of course, without transaction, the update and select may not get the same row if someone changes it in between your 2 statement.

A trick with user variable, if the update changes only 1 row, you can use this.
UPDATE table SET id=(@tempid:=id) , .... LIMIT 1;
SELECT @tempid;
This will not be affected by changes from other connections.

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: LAST_INSERT_ID() AND UPDATE STATEMENTS
5545
May 27, 2007 04:38PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.