mysql - Inserting an automatically generated index from a table into another table -


say have 2 tables:

  • table1 (id, name, type) id primary key, , auto-incremented
  • table2 (item_id, place) item_id refers id in table1

i want following:

insert table1(name, type) values (y, z); 

assuming that query assign id=x row, then:

insert table2(item_id, place) values (x, w); 

how that? in other words, how id of row has been added?

insert table1(name, type) values (y, z); insert table2(item_id, place) values (last_insert_id() , w); 

the last_insert_id() you. programming languages have special function call you, works in pure mysql.


Comments