If you do an INSERT and you generate an new number in an AUTO_INCREMENT
field the new value generated can be retrieved using:
*SELECT LAST_INSERT_ID();*

You are not required to retrieve into the application and then use it back
in another SQL statement... you can use user variables:
*SELECT @lastGenerated:=LAST_INSERT_ID();*

And then use: the variable in another query:
*INSERT INTO someTbl (id, someCol, somCol2) VALUES (NULL, @lastGenerated,
'OutsideValue');
*
Be warned that LAST_INSERT_ID() is functional only if you did not specified
the AUTO_INCREMENT field's value... only if you did not specified the
AUTO_INCREMENT field at all... or in the insert statement you used NULL or 0
as a value... those values are considered "magic" by MySQL in case of an
AUTO_INCREMENT field.

Now part 2... if you didn't created an AUTO_INCREMENT field... and only want
to use the last value from that table:
Either you use a MAX() request:
*SELECT @lastGenerated:=MAX(columnName) FROM tableName*
Or:
*SHOW TABLE STATUS LIKE 'tableName'*
and somewhere in the result is the AUTO_INCREMENT value.

Good luck !

--
Gabriel PREDA
Senior Web Developer
  • Help Needed Bhanu Prakash
    • Re: Help Needed Gabriel PREDA

Reply via email to