我的SQL语句如下:
insert into foo (val1) values ('v1'); # ID in first table
foo_id = select last_insert_id();
insert into bar (val2) values ('v2'); # ID in second table
bar_id = select last_insert_id();
insert into foobar (foo_id, bar_id, val3) values (foo_id, bar_id,'text'); # third table
以上不起作用,它无法识别foo_id =语句。
答案 0 :(得分:4)
insert into foo (val1) values ('v1'); -- ID in first table
SET @foo_id = last_insert_id();
insert into bar (val2) values ('v2'); -- ID in second table
SET @bar_id = last_insert_id();
insert into foobar (foo_id, bar_id, val3) values (@foo_id, @bar_id,'text'); -- third table