[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1462
  • Last Modified:

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
0
JUANFRANPEREZ
Asked:
JUANFRANPEREZ
1 Solution
 
snoyes_jwCommented:
SELECT LAST_INSERT_ID();
0
 
NovaDenizenCommented:
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?
0
 
BrianPap22Commented:
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.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
KennyTMCommented:
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.
0
 
JUANFRANPEREZAuthor Commented:
Ok, LAST_INSERT_ID is what i need. I want to know the last record Id i´ve just inserted.
Tx all
0
 
NovaDenizenCommented:
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.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now