Get the primary key after a DUPLICATE KEY error?

If I run an insert request but it fails due to a duplicate key error, is there a way to get my primary key without making another choice?

Basically:

INSERT INTO tbl (field) VALUES ('myvalue')

This fails because there is already a record with identifier: 1 and field: myvalue.

Now I want to know that ID: 1 without making another request:

SELECT id FROM tbl WHERE field = 'myvalue'

is it possible?

+3
source share
2 answers

Here is a link that provides four different ways to handle this: http://mikefenwick.com/blog/insert-into-database-or-return-id-of-duplicate-row-in-mysql/

+2
source

Each library has a condition to get the identifier of the row that was inserted.

: mysql_query() mysql_insert_id() ,

EDIT:

, , field.

, field

0

All Articles