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