Get data from the last entry on a table

Posted on 2006-05-18
Last Modified: 2008-05-12

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
    LVL 33

    Accepted Solution

    LVL 22

    Expert Comment

    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?
    LVL 4

    Expert Comment

    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.
    LVL 8

    Expert Comment

    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.

    Author Comment

    Ok, LAST_INSERT_ID is what i need. I want to know the last record Id i´ve just inserted.
    Tx all
    LVL 22

    Expert Comment

    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.

    There are analagous calls in the other mysql APIs.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now