Let's suppose you have a table `t` with these columns:
id -> auto increment, primary key, not null
a
b
c
....
N

If you do any of these:
INSERT INTO `t` (a, b, c ..., K) VALUES(va, vab, vc, ... vK);
INSERT INTO `t` (id, a, b, c ..., K) VALUES(0, va, vab, vc, ... vK);
INSERT INTO `t` (id, a, b, c ..., K) VALUES(NULL, va, vab, vc, ... vK);

In the above cases MySQL "generates" the ID for you... and you may
retrieve it immediately after the query that generates it with:
SELECT @LastGeneratedId := LAST_INSERT_ID();

Or you can use it in a 2 contigous INSERTS:
INSERT INTO `t` (id, a, b, c ..., K) VALUES(NULL, va, vab, vc, ... vK);
INSERT INTO `t2` VALUES(LAST_INSERT_ID(), f, g, h);


Have fun !

But not that LAST_INSERT_ID will not be updated if you insert an
explicit value (except: 0)
INSERT INTO `t` (id, a, b, c ..., K) VALUES(145899, va, vab, vc, ...
vK); <---- this will not affect LAST_INSERT_ID() value.

And another thing... LAST_INSERT_ID() is kept on a per connection
basis... so it will not mix with other users LAST_INSERT_IDs

-- -- -- -- -- -- -- -- -- -- -- -- -- --
Gabriel PREDA
Senior Web Developer

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to