-1

I have an auto-increment column 'id' . Now I want to echo the last generated id. I have tried these queries -
SELECT MAX(id) FROM table_name
SELECT id from table_name ORDER BY id DESC LIMIT 1
SELECT LAST_INSERT_ID();

Above two queries returning the Max Id which is actually the last id generated. But the problem is suppose if I delete a row where id is 10, then again I insert a row. Then the id would be 11 in the last row, not 10. But these 2 queries are returning the value 9. But I want the value 10. LAST_INSERT_ID() should be used after an insert query which I dont need to do.

How can I get the id value of 10 ?

A K Pal
  • 59
  • 1
  • 1
  • 6

1 Answers1

0
SELECT `AUTO_INCREMENT`
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND   TABLE_NAME   = 'TableName';

This was taken from Get current AUTO_INCREMENT value for any table

Community
  • 1
  • 1
SilicaGel
  • 459
  • 3
  • 11