I am trying to create a many to many relationship between 2 tables. I’ve got 3 tables for that. It follows TOXY model.
table a: a.id (primary key)
table ab: ab.a_id (foreign key) ab.b_id (foreign key)
table b: b.id (primary key)
How should I insert the data so it will be all linked up?
Like this? "INSERT INTO a ('name') VALUES ('my name')";
then like this? "INSERT INTO b ('name') VALUES ('my name')";
but then I have to have the a.id and b.id to put it in table ab. How should I retrieve them?
i know i could do a SELECT a.id FROM a WHERE name = ‘my name’. but isnt there an easier way for this that automatically returns an id when u INSERT the row?
All you need to do is store those IDs in variables to use in a query to insert into the
abtable.LAST_INSERT_ID()returns the ID of the inserted rows. So, in PHP for instance: