Table a has a one-to-many relationship with table b.
table a (id)
table b (id, aId -> a.id)
Both table a and table b have autoincrementing ids.
I run the following statements in a single call:
insert into a values ();
set @id = last_insert_id();
insert into b (aId) values (@id);
insert into a values ();
set @id = last_insert_id();
insert into b (aId) values (@id);
insert into a values ();
set @id = last_insert_id();
insert into b (aId) values (@id);
...and here is the result:
select * from b
id aId
1 3
2 3
3 3
This is not what I was expecting. Here's what I would expect to see:
id aId
1 1
2 2
3 3
So it seems like only the last last_insert_id is used? After some experimenting, I find I can get the results I want, if I use a different variable name after each insert.
insert into a values ();
set @id1 = last_insert_id();
insert into b (aId) values (@id1);
insert into a values ();
set @id2 = last_insert_id();
insert into b (aId) values (@id2);
insert into a values ();
set @id3 = last_insert_id();
insert into b (aId) values (@id3);
However I was hoping to just have a single variable name, and sequentially reassign it as needed.
Is this possible?