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

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

        <legend id='gkGqZ'><style id='gkGqZ'><dir id='gkGqZ'><q id='gkGqZ'></q></dir></style></legend>

        LAST_INSERT_ID() MySQL

        时间:2023-08-17
        • <legend id='Ysw13'><style id='Ysw13'><dir id='Ysw13'><q id='Ysw13'></q></dir></style></legend>
          • <tfoot id='Ysw13'></tfoot>

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

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

                • 本文介绍了LAST_INSERT_ID() MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 MySQL 问题,我认为它一定很简单.当我运行以下 MySql 查询时,我需要从 table1 返回 LAST INSERTED ID:

                  I have a MySQL question that I think must be quite easy. I need to return the LAST INSERTED ID from table1 when I run the following MySql query:

                  INSERT INTO table1 (title,userid) VALUES ('test',1); 
                  INSERT INTO table2 (parentid,otherid,userid) VALUES (LAST_INSERT_ID(),4,1);
                  SELECT LAST_INSERT_ID();
                  

                  如您所知,当前代码只会返回 table2 的 LAST INSERT ID 而不是 table1,即使我插入 table2 之间,我如何从 table1 获取 ID?

                  As you can understand the current code will just return the LAST INSERT ID of table2 instead of table1, how can I get the id from table1 even if I insert into table2 between?

                  推荐答案

                  您可以将最后一个插入 id 存储在一个变量中:

                  You could store the last insert id in a variable :

                  INSERT INTO table1 (title,userid) VALUES ('test', 1); 
                  SET @last_id_in_table1 = LAST_INSERT_ID();
                  INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1);    
                  

                  或者从 table1 中获取最大 id(警告.请参阅 Rob Starling 评论中的注释,关于使用最大 id 时竞争条件可能导致的错误)

                  Or get the max id from table1 ( Warning. See note in comments from Rob Starling about possible errors from race conditions when using the max id)

                  INSERT INTO table1 (title,userid) VALUES ('test', 1); 
                  INSERT INTO table2 (parentid,otherid,userid) VALUES (LAST_INSERT_ID(), 4, 1); 
                  SELECT MAX(id) FROM table1;  
                  

                  (警告:正如 Rob Starling 在

                  (Warning: as Rob Starling points out in the

                  这篇关于LAST_INSERT_ID() MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL:启用 LOAD DATA LOCAL INFILE 下一篇:MySQL 中的 NULL(性能和存储)

                  相关文章

                  最新文章

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

                    <tfoot id='syZSz'></tfoot>
                      <legend id='syZSz'><style id='syZSz'><dir id='syZSz'><q id='syZSz'></q></dir></style></legend>
                      • <bdo id='syZSz'></bdo><ul id='syZSz'></ul>

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