So theres lots of questions and answers about LAST_INSERT_ID(). 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?
Take this table:
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
I need to insert the value of Name 1
into a table, and then take that primary key
and insert the values from Data1, 2 and 3 into other tables that have a primary key constraint over them.
The problem is that "Name" is non-unique so I can't query back the original/staging table and get the values that way.
As per the documentation, last_insert_id() operates in the context of a single INSERT statement. I tested it anyway.
I also tried writing the value back into the table, but of course SQL operates on sets:
insert into test_fetch_id(lastname,Last_ID)
select RESOURCE_ID, last_insert_id() from resources r limit 100;
Giving me:
Any help would be greatly appreciated.