JUANFRANPEREZ
asked on
Get data from the last entry on a table
Hi
i want to know the value of the last record inserted on a table, just before the insert into. It has an auto inc value that i need to know.
Tx all
i want to know the value of the last record inserted on a table, just before the insert into. It has an auto inc value that i need to know.
Tx all
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Most mysql APIs have a call to tell you the last insert id of the auto_increment field of the record you just inserted. However, I think you're asking about the id of the record that was most recently inserted, not necessarily by you. Which exactly are you looking for?
SELECT ID from tablename WHERE ID = MAX(ID);
LAST_INSERT_ID doesn't seem like it's what you want, since it's only valid during your specific session with the server.
LAST_INSERT_ID doesn't seem like it's what you want, since it's only valid during your specific session with the server.
In fact
SELECT MAX(ID) from tablename
is enough if you wanna fetch the ID only. To fetch the whole row use the "WHERE ID = MAX(ID)" clause like BrianPap22'd suggested.
SELECT MAX(ID) from tablename
is enough if you wanna fetch the ID only. To fetch the whole row use the "WHERE ID = MAX(ID)" clause like BrianPap22'd suggested.
ASKER
Ok, LAST_INSERT_ID is what i need. I want to know the last record Id i´ve just inserted.
Tx all
Tx all
MySQL returns this number as part of normal return status for a query. For instance in the C API, after you use mysql_real_query() to insert an AUTO_INCREMENT record, you can call mysql_insert_id() to instantly get the last insert id without sending another query out on the wire. http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html
There are analagous calls in the other mysql APIs.
There are analagous calls in the other mysql APIs.