title: MySQL - Return group of last inserted ID's
body: <p>So theres lots of questions and answers about <a href="https://stackoverflow.com/questions/20891875/how-to-get-last-inserted-id-from-table-mysql">LAST_INSERT_ID()</a>.  This works fine if you're inserting a single row and need the return value of that ID to do stuff with, but what if you are doing a group insert and need the return values of all AUTO_INCREMENT ID's?</p>
  
  <p>Take this table:</p>
  
  <pre><code>Name     |   Data1    |    Data2    |      Data3
  ---------------------------------------------------
  Name 1   |   blah     |    Herp     |      Derp
  Name 2   |   rwwe     |    asdf     |      3445
  Name 3   |   asdf     |    asdf     |      8678
  Name 1   |   guih     |    adsf     |      3565
  Name 1   |   asfg     |    rwer     |      7877
  </code></pre>
  
  <p>I need to insert the value of <code>Name 1</code> into a table, and then take that <code>primary key</code> and insert the values from Data1, 2 and 3 into other tables that have a primary key constraint over them.</p>
  
  <p>The problem is that "Name" is non-unique so I can't query back the original/staging table and get the values that way.</p>
  
  <p>As per the documentation, <a href="https://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id" rel="nofollow noreferrer">last_insert_id()</a> operates in the context of a single INSERT statement.  I tested it anyway.</p>
  
  <p><img src="https://i.stack.imgur.com/5VjoA.png" alt="enter image description here"></p>
  
  <p>I also tried writing the value back into the table, but of course SQL operates on sets:</p>
  
  <pre><code>insert into test_fetch_id(lastname,Last_ID)
  select RESOURCE_ID, last_insert_id() from resources r limit 100;
  </code></pre>
  
  <p>Giving me:
  <img src="https://i.stack.imgur.com/dtRQA.png" alt="enter image description here"></p>
  
  <p>Any help would be greatly appreciated.</p>