Answer the question
In order to leave comments, you need to log in
How to immediately get the AUTO_INCREMENT id of a duplicate record on INSERT?
A frequent case when you need to insert a record, and if there is already a record with such a UNIQ key, then return the AUTO_INCREMENT ID field for this record.
Now I'm doing it in the forehead:
if(mysql_query("INSERT INTO tab (hash) VALUE ('hash')"))
{
if(mysql_errno()== ER_DUP_KEY)
{
mysql_query("SELECT id FROM tab WHERE hash = 'hash'");
}
}
Answer the question
In order to leave comments, you need to log in
Let's say this:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question