mohamad mohamad mohamad mohamad - 2 months ago 15
MySQL Question

how to get from database the last inset id before insert

How to select the last insert id from database table

i need to get the last insert id from database

i try the

last_insert_id()
function but this doesn't work for me because i want to get this id before i do an insert query and when i get this id i do an insert query

i try to use
max
for the id but this have a problem that if i have a deleted
id it will not be taken


example if i have id 10 deleted than the max id will be 9 but the auto
increment will be 11


than i want to have the new id that will be added by the auto increment


example:i have an id 10 deleted i want to take than next id it will be
11

Answer

You Can :

    SELECT AUTO_INCREMENT
    FROM  INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = 'database_name'
    AND   TABLE_NAME   = 'table_name';
And get your last id.

Comments