title: MYSQL: Getting existing primary key when inserting record with duplicate unique key?
body: <p>I've got a mysql database with a table that has both a auto-increment primary key and unique string valued key (a sha-1 hash). </p>
  
  <p>If I try to add a record that has the same sha-1 hash as an existing record, I just want to get the primary key of the existing record. I can use something like "INSERT ... ON DUPLICATE KEY UPDATE" or "INSERT IGNORE" to prevent an exception when trying to insert a record with a existing hash value. </p>
  
  <p>However, when that happens, I need to retrieve the primary key of the existing record. I can't find a way to do that with a single SQL statement. If it matters, my code is in Java and I'm using JDBC. </p>
  
  <p>Alternatively, I can do it with two statements (either a query followed by an insertion if not found, or a insertion followed by a query if a duplicate key exists). But I presume a single statement would be more efficient.</p>