title: Retrieving all LAST_INSERT_ID of MySQL batch INSERT ... ON DUPLICATE UPDATE ...?
body: <p>Current rows in <code>t1</code>:</p>
  <pre><code>id,f1,f2,f3
  1,'01','02','03'
  2,'11','12','13'
  4,'41','42','43'
  </code></pre>
  <p>Wherein <code>f1</code> is a unique key field.</p>
  <p>Now we perform this query:</p>
  <pre><code>INSERT INTO `t1` (`f1`, `f2`, `f3`) VALUES 
  ('11', '12', '13'),
  ('21', '22', '23'),
  ('31', '32', '33'),
  ('41', '42', '43'),
  ('51', '52', '53')
  ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)
  </code></pre>
  <p>How do I get the last insert IDs of all the rows inserted/updated?</p>
  <p>For the example above, I believe the IDs would be:</p>
  <p>2
  5
  6
  4
  7</p>
  <p>Right?</p>
  <p>But how do I get them from MySQL after a batch INSERT query with ON DUPLICATE KEY UPDATE clause like this?</p>
  <p>From <a href="https://stackoverflow.com/a/1285278/49318">this answer</a>, LAST_INSERT_ID &quot;gives you the id of the FIRST row inserted in the last batch&quot; and one can simply get all the rest IDs in a sequential manner if the rows are all freshly INSERT-ed.</p>
  <p>Apparently, when we have ON DUPLICATE KEY UPDATE, it's a totally different story. So is there any way to get all the last insert IDs of a batch INSERT with ON DUPLICATE KEY UPDATE? Tried google but found nothing helpful.</p>
  <h1>Why do I need this</h1>
  <p>After inserting in <code>t1</code>, I need to associate the rows just inserted with one of the rows in <code>t2</code>. It's a many-to-many relationship so I would need to insert in <code>t1_x_t2</code> with the ID of the row from <code>t2</code> and all the IDs just inserted in <code>t1</code>.</p>
  <p>Is there any other way to achieve this?</p>