If a table contains an
AUTO_INCREMENT
column andINSERT ... UPDATE
inserts a row, theLAST_INSERT_ID()
function returns theAUTO_INCREMENT
value. If the statement updates a row instead,LAST_INSERT_ID()
is not meaningful. However, you can work around this by usingLAST_INSERT_ID(expr)
. Suppose that id is theAUTO_INCREMENT
column. To makeLAST_INSERT_ID()
meaningful for updates, insert rows as follows:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
From http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html