vinnylinux
vinnylinux

Reputation: 7034

Discover last insert ID of autoincrement column in MySQL

I'm currently using:

SELECT MAX(id) FROM table

To discover the current id of a certain table, but i heard this can bring bad results. What is the proper way of doing that? Please, notice that i'm not INSERTING or DELETING anything before that query. I just want to know the current ID, without prior INSERT or DELETE.

Upvotes: 2

Views: 539

Answers (2)

Klaus S.
Klaus S.

Reputation: 1219

You can use the following query:

SELECT id FROM table ORDER BY id DESC LIMIT 1;

Upvotes: 1

Alan
Alan

Reputation: 184

Perform the following SQL:

SHOW TABLE STATUS LIKE 'TABLENAME'

Then check field AUTO_INCREMENT

Upvotes: 2

Related Questions