On Thursday 12 December 2002 07:23, Max Clark wrote: > I would like to run an insert query across two tables at the same time. The > first table has a primary key that is auto_increment, the second table > needs to insert the primary key from the first table as a reference? > > How do I auto-populate the tableId field with the correct entry from the > first table insert? > > Thanks in advance, > > Max > > insert into table1 (name, desc) values ("foo", "foouser"); > > id name desc > > 55 foo foouser > > insert into table2 (table1Id, text) values ("?", "some text"); > > id table1Id text > > 69 55 some text
Use LAST_INSERT_ID() function: http://www.mysql.com/doc/en/Miscellaneous_functions.html http://www.mysql.com/doc/en/example-Foreign_keys.html -- For technical support contracts, goto https://order.mysql.com/?ref=ensita This email is sponsored by Ensita.net http://www.ensita.net/ __ ___ ___ ____ __ / |/ /_ __/ __/ __ \/ / Victoria Reznichenko / /|_/ / // /\ \/ /_/ / /__ [EMAIL PROTECTED] /_/ /_/\_, /___/\___\_\___/ MySQL AB / Ensita.net <___/ www.mysql.com --------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php