mysql> insert into foo (name) VALUES ("foo") on duplicate key update name=name, id=LAST_INSERT_ID(id); Query OK, 0 rows affected (0.00 sec) mysql> select LAST_INSERT_ID(); +------------------+ | LAST_INSERT_ID() | +------------------+ | 1 | +------------------+ 1 row in set (0.00 sec)