I have a table with has the possibility of a parent child relationship. "parentid" has a foreign key to it's parent's "dataid" for example:
dataid dataname parentid
---------------------------------
1 example NULL
2 record NULL
3 foo 1
4 bar 1
In this example population "foo" and "bar" are children of "example". Now i would like to insert multiple data records into this table, since dataid is auto incremented i would like to know if i can get this id after my insert without executing an extra query. So for example:
INSERT INTO table (dataname, parentid) VALUES
( cat, NULL ), ( tiger, **dataid from cat** ), ( lion, **dataid from cat** );
To get this result:
dataid dataname parentid
---------------------------------
5 cat NULL
6 tiger 5
7 lion 5
ANSWER -- Thanks to PLB (and Simon Cambier):
Still executing two queries after each other but i think this is solution will do.
/*first insert parent*/
INSERT INTO table (dataname, parentid) VALUES
( cat, NULL );
/*insert children*/
INSERT INTO table (dataname, parentid) VALUES
( tiger, LAST_INSERT_ID() ), ( lion, LAST_INSERT_ID() );