【问题标题】:How do I get all the ids of the row created by one multiple row insert statement如何获取由一个多行插入语句创建的行的所有 id
【发布时间】:2023-03-11 04:30:02
【问题描述】:

我是 php 新手。所以,如果这似乎是一个愚蠢的问题,请原谅我。

假设我有一个 MySQL 插入语句 insert into table (a,b) values (1,2),(3,4),(5,6)。表 'table' 有一个名为 'id' 的自动增量字段。

如何检索上面插入语句创建的所有 id?

如果我能得到一个使用 mysqli 的例子,那就太好了。

【问题讨论】:

  • 如果你知道你插入了多少,也许你可以用 MAX(id) 求和查询做点什么?
  • 你为什么需要所有这些id?你插入的数据是什么?
  • @Nitrodist 哦,不!写上千遍:id 不是数字!
  • @Col。 Sharpnel:我使用 php 作为 flex 客户端的后端。所以,我需要发回我刚刚创建的所有对象的列表。为此,我需要我刚刚创建的项目的 ID,以便我可以运行选择查询并构建结果
  • Myabe SELECT ID FROM table WHERE id > MAX(ID) - 3;

标签: php mysql insert mysqli


【解决方案1】:

在某些情况下,如果您有另一个排序标识符,例如 UserID,您可以通过 UniqueID 大于或等于 mysql_insert_id() 来过滤查询,限制受影响的行数并仅显示用户。这实际上只适用于事务内部。

$SQL = "INSERT INTO Table
       (UserID, Data)
       VALUES
       (1,'Foo'),
       (1,'Bar'),
       (1,'FooBar')";

$Result = mysql_query($SQL);
$LastID = mysql_insert_id();
$RowsAffected = mysql_affected_rows();

$IDSQL = "SELECT RecordID
          FROM Table
          WHERE UserID = 1
          AND RecordID >= '$LastID' 
          LIMIT '$RowsAffected'";
$IDResult = mysql_query($IDSQL);

【讨论】:

    【解决方案2】:

    也许我可以这样做

    $insert = "insert into table (a,b) values (1,2),(3,4),(5,6)";
    $mysqli->query($insert);
    $rows_to_be_inserted=3;
    $inserted_id = $mysqli->insert_id // gives me the id of the first row in my list
    $last_row_id = ($inserted_id+$rows_to_be_inserted)-1;
    $mysql->query("select * from table where id between  $inserted_id and $last_row_id");
    

    你们说什么?

    【讨论】:

      【解决方案3】:

      AngeDeLaMort 的回答几乎是正确的。当然,处理该问题最合适的方法是一次插入一行并轮询 insert_id 或在其他地方生成序列(这在可伸缩性方面具有额外的好处)。

      我强烈建议不要尝试确定最后一个 insert_id 并将其与插入后 后的最新 insert_id 进行比较 - 这很可能会失败。

      但是...另一种方法是:

      ....
      "INSERT INTO destn (id, data, other, trans_ref) 
       SELECT id, data, other, connection_id() FROM source";
      ....
      "SELECT id FROM destn WHERE trans_ref=connection_id()";
      ....
      "UPDATE destn SET trans_ref=NULL where trans_ref=connection_id()";
      

      第二个查询将返回生成的 ID(请注意,这假设您对所有 3 个查询使用相同的连接)。第三个查询是必要的,因为连接 ID 在您断开连接时会返回到池中(即被重用)。

      C.

      【讨论】:

        【解决方案4】:

        作为 AngeDeLaMort 的后续行动: 您可以分开插入并执行以下操作:

        $data = array (
            array(1,2),
            array(3,4),
            array(5,6)
        );
        $ids = array();
        
        foreach ($data as $item) {
           $sql = 'insert into table (a,b) values ('.$item[0].','.$item[1].')';
           mysql_query ($sql);
           $id[] = mysql_insert_id();
        }
        

        现在你所有的新 id 都在 $id 数组中了。

        【讨论】:

        • 我也在想这个。但是效率不高吗?我的意思是多次访问数据库?
        【解决方案5】:

        你不能。我建议您维护自己的 ID(使用 guid 或您自己的自动增量表)并在插入表时使用它。

        但可以使用 LAST_INSERT_ID() 获取最后插入的自动增量值:

        http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

        【讨论】:

        • 我不相信 last_insert_id() 因为可能有一些其他请求会将行插入其他表。如果我错了,请纠正我。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-15
        • 1970-01-01
        • 2013-05-08
        • 2013-10-11
        • 2019-11-21
        • 2011-11-22
        相关资源
        最近更新 更多