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 ?