zoukankan      html  css  js  c++  java
  • How Can I Get the Unique ID for the Last Inserted Row

    24.1.4.3 How Can I Get the Unique ID for the Last Inserted
    Row?

    /*********** the following is from manual of MySQL ************/
    If you insert a record in a table containing a column that has the AUTO_INCREMENT attribute,
    you can get the most recently generated ID by calling the mysql_insert_id() function.
    You can also retrieve the ID by using the LAST_INSERT_ID() function in a query string
    that you pass to mysql_query().
    You can check if an AUTO_INCREMENT index is used by executing the following code. This
    also checks if the query was an INSERT with an AUTO_INCREMENT index:

    if (mysql_error(&mysql)[0] == 0 &&
    mysql_num_fields(result) == 0 &&
    mysql_insert_id(&mysql) != 0)
    {
    used_id = mysql_insert_id(&mysql);
    }



    The most recently generated ID is maintained in the server on a per-connection basis. It
    will not be changed by another client. It will not even be changed if you update another
    AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not
    0).
    If you want to use the ID that was generated for one table and insert it into a second table,
    you can use SQL statements like this:

    INSERT INTO foo (auto,text)
    VALUES(NULL,’text’);            # generate ID by inserting NULL
    INSERT INTO foo2 (id,text)
    VALUES(LAST_INSERT_ID(),’text’);      # use ID in second table
  • 相关阅读:
    AI换脸必备知识:如何查看显卡型号以及显存大小!
    DeepFaceLab620稳定版使用过程详解!
    DeepFaceLab错误:DLL Load failed 找不到指定模块!
    DeepFaceLab进阶:H128,DF,SAE模型有何不同?哪个最好?
    J2EE与EJB
    Servlet与JSP
    Java网络编程详解
    Java多线程详解
    Java数据库操作
    Java多线程
  • 原文地址:https://www.cnblogs.com/no7dw/p/2310047.html
Copyright © 2011-2022 走看看