On Sat, Mar 31, 2001 at 10:11:33PM +0530, P.V. Sajan wrote:
> Helllo All !!
>
> I 've a problem i want to retrieve the primary key value of the last
> Inserted record in a table and use it in another table.
>
> I did
> select Last_Insert_Id () from trans ;
What you were doing is like:
select 19 from trans;
which would result in a row of 19 for each row in the trans
table.
Just do:
select last_insert_id();
Or use last_insert_id() in place of the new id in any other query.
E.g.
insert into trans2 values(last_insert_id(), 'test');
Which will insert (19, 'test') into trans2 if 19 was the last
id generated for a new record in trans for example.
The last auto_increment id is not kept per table so you should
retrieve it before inserting in any other table. It IS kept per
connection, so you're safe from other programs that insert at
the same time.
Regards,
Fred.
--
Fred van Engen XO Communications B.V.
email: [EMAIL PROTECTED] Televisieweg 2
tel: +31 36 5462400 1322 AC Almere
fax: +31 36 5462424 The Netherlands
---------------------------------------------------------------------
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