Link to home
Start Free TrialLog in
Avatar of SweatCoder
SweatCoderFlag for United States of America

asked on

last_insert_id

I need to know the last auto_increment value inserted into a given table. in my experiments with last_insert_id, it seems this gives the last id value anywhere in the database, which is useless. is there a way to get the last id value for a specific table?

i can't use max(id) because there are times when the last record was deleted, and i don't want to get the highest id currently in the table, but the highest id that was *ever* in the table.
Avatar of cracky
cracky

If you use LAST_INSERT_ID() directly after the last query, then it will give you the right figure, since it calculates on a per-connection basis. See:

http://www.mysql.com/doc/en/Getting_unique_ID.html

If you want to get the most recently inserted row after you have closed a connection and re-opened it, I can only think you need to have a date_created(TIMESTAMP) column on your table and SELECT MAX(date_created) from it to get this result.
ASKER CERTIFIED SOLUTION
Avatar of Aleksandar Bradarić
Aleksandar Bradarić
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SweatCoder

ASKER

leannonn, you came through for me again. that's exactly what i needed.
All you have to do is ask O;)
leannonn: I need you over here:
https://www.experts-exchange.com/questions/20890859/date-comparisons.html#10404157

sorry, but this site offers no other way to contact somebody!!!

thanks.