• <tfoot id='y9zte'></tfoot>
  • <i id='y9zte'><tr id='y9zte'><dt id='y9zte'><q id='y9zte'><span id='y9zte'><b id='y9zte'><form id='y9zte'><ins id='y9zte'></ins><ul id='y9zte'></ul><sub id='y9zte'></sub></form><legend id='y9zte'></legend><bdo id='y9zte'><pre id='y9zte'><center id='y9zte'></center></pre></bdo></b><th id='y9zte'></th></span></q></dt></tr></i><div id='y9zte'><tfoot id='y9zte'></tfoot><dl id='y9zte'><fieldset id='y9zte'></fieldset></dl></div>

    1. <small id='y9zte'></small><noframes id='y9zte'>

        <bdo id='y9zte'></bdo><ul id='y9zte'></ul>
      1. <legend id='y9zte'><style id='y9zte'><dir id='y9zte'><q id='y9zte'></q></dir></style></legend>

        从 MySQL 插入查询中获取新记录主键 ID?

        时间:2023-08-18

          <i id='Q6EOg'><tr id='Q6EOg'><dt id='Q6EOg'><q id='Q6EOg'><span id='Q6EOg'><b id='Q6EOg'><form id='Q6EOg'><ins id='Q6EOg'></ins><ul id='Q6EOg'></ul><sub id='Q6EOg'></sub></form><legend id='Q6EOg'></legend><bdo id='Q6EOg'><pre id='Q6EOg'><center id='Q6EOg'></center></pre></bdo></b><th id='Q6EOg'></th></span></q></dt></tr></i><div id='Q6EOg'><tfoot id='Q6EOg'></tfoot><dl id='Q6EOg'><fieldset id='Q6EOg'></fieldset></dl></div>
          <tfoot id='Q6EOg'></tfoot>

            <bdo id='Q6EOg'></bdo><ul id='Q6EOg'></ul>

            <small id='Q6EOg'></small><noframes id='Q6EOg'>

                  <tbody id='Q6EOg'></tbody>
                • <legend id='Q6EOg'><style id='Q6EOg'><dir id='Q6EOg'><q id='Q6EOg'></q></dir></style></legend>

                  本文介绍了从 MySQL 插入查询中获取新记录主键 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我正在我的一个表中执行 MySQL INSERT 并且该表具有设置为 autoincrement 的列 item_id主键.

                  Let's say I am doing a MySQL INSERT into one of my tables and the table has the column item_id which is set to autoincrement and primary key.

                  如何让查询在同一个查询中输出新生成的主键item_id的值?

                  How do I get the query to output the value of the newly generated primary key item_id in the same query?

                  目前我正在运行第二个查询来检索 ID,但考虑到这可能会产生错误的结果,这似乎不是一个好习惯......

                  Currently I am running a second query to retrieve the id but this hardly seems like good practice considering this might produce the wrong result...

                  如果这是不可能的,那么确保我检索到正确 ID 的最佳做法是什么?

                  If this is not possible then what is the best practice to ensure I retrieve the correct id?

                  推荐答案

                  需要使用LAST_INSERT_ID()函数:http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-插入-id

                  例如:

                  INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...);
                  SELECT LAST_INSERT_ID();
                  

                  这将使您返回插入的最后一行的PRIMARY KEY值:

                  This will get you back the PRIMARY KEY value of the last row that you inserted:

                  生成的 ID 以每个连接为基础在服务器中维护.这意味着函数返回给给定客户端的值是为该客户端影响 AUTO_INCREMENT 列的最新语句生成的第一个 AUTO_INCREMENT 值.

                  The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client.

                  因此,LAST_INSERT_ID() 返回的值是针对每个用户的,并且不受其他用户在服务器上运行的其他查询的影响.

                  So the value returned by LAST_INSERT_ID() is per user and is unaffected by other queries that might be running on the server from other users.

                  这篇关于从 MySQL 插入查询中获取新记录主键 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:计算 DISTINCT 值的出现次数 下一篇:子查询与连接

                  相关文章

                  最新文章

                  <legend id='ZYkvm'><style id='ZYkvm'><dir id='ZYkvm'><q id='ZYkvm'></q></dir></style></legend>
                  <i id='ZYkvm'><tr id='ZYkvm'><dt id='ZYkvm'><q id='ZYkvm'><span id='ZYkvm'><b id='ZYkvm'><form id='ZYkvm'><ins id='ZYkvm'></ins><ul id='ZYkvm'></ul><sub id='ZYkvm'></sub></form><legend id='ZYkvm'></legend><bdo id='ZYkvm'><pre id='ZYkvm'><center id='ZYkvm'></center></pre></bdo></b><th id='ZYkvm'></th></span></q></dt></tr></i><div id='ZYkvm'><tfoot id='ZYkvm'></tfoot><dl id='ZYkvm'><fieldset id='ZYkvm'></fieldset></dl></div>
                    <bdo id='ZYkvm'></bdo><ul id='ZYkvm'></ul>

                    <small id='ZYkvm'></small><noframes id='ZYkvm'>

                    <tfoot id='ZYkvm'></tfoot>