andrisp Posted November 17, 2006 Report Share Posted November 17, 2006 (edited) Kā vislabāk veikt insertu divās tabulās, ja otrā tabulā jāizmanto pirmajā tabulā uzģenerētais primary key ? Vienīgais ko es varēju izdomāt: SET @last_tabula1_id = null; LOCK TABLES t1 WRITE, t2 WRITE; INSERT INTO `t1` () VALUES (); SELECT @last_tabula1_id := MAX(id) FROM `t1`; INSERT INTO `t2` (id, t1_id) VALUES ('', @last_tabula1_id); UNLOCK TABLES; Vai nav kāds vienkāršāks un labāks veids ? PS. Atvainojos par nesakarīgo topika nosaukumu - apgraizījās man nepamanot :) Edited November 17, 2006 by andrisp Link to comment Share on other sites More sharing options...
Vebers Posted November 17, 2006 Report Share Posted November 17, 2006 mysql_query(" [sql]INSERT INTO t1 () VALUES ();[/sql] ); $last_id = mysql_insert_id(); mysql_query(" [sql]INSERT INTO t2 (id, t1_id) VALUES ('', ".$last_id [/sql]); Link to comment Share on other sites More sharing options...
andrisp Posted November 17, 2006 Author Report Share Posted November 17, 2006 (edited) Nu tas būs tas pats, bet tikai bez tabulu lokošanām. UPD: Piem., ja to visu kautkā varētu savienot vienā kverīja, tad nebūtu jāloko tabula. Edited November 17, 2006 by andrisp Link to comment Share on other sites More sharing options...
bubu Posted November 17, 2006 Report Share Posted November 17, 2006 Un kāpēc lai tu gribētu lokot tabulas? last_insert_id jau ir lokāls konekcijai, nevis globāls uz pēdējo insertu. Link to comment Share on other sites More sharing options...
andrisp Posted November 17, 2006 Author Report Share Posted November 17, 2006 To palaidu garām. Link to comment Share on other sites More sharing options...
Recommended Posts